vyclarks
vyclarks

Reputation: 878

Error when get data from xml file

I have profile.xml file in my web folder:

<?xml version="1.0" encoding="utf-8"?>
<myXML>
  <RealName>Nguyen Van A</RealName>
  <Email>[email protected]</Email>
  <Phone>2165421</Phone>
  <Address>Ho Chi Minh</Address>
  <Link1>dtvt</Link1>
  <Link2></Link2>
  <Link3></Link3>
</myXML>

As recommended, I get data from that file by code below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
 public class profile
    {
        public string realname { get; set; }
        public string email { get; set; }
        public string phone { get; set; }
        public string address { get; set; }
        public string link1 { get; set; }
        public string link2 { get; set; }
        public string link3 { get; set; }
    }

 public void getProfile()
    {
        string path = this.Server.MapPath("~/Lecturer/");
        string targetPath = path + @"\"+username+"\\profile.xml";

        bool isExists = System.IO.Directory.Exists(targetPath);

        if(isExists)
        {
            List<profile> profiles = (
                                         from e in XDocument.Load(targetPath)
                                                            .Root.Element("myXML")
                                         select new profile
                                                    {

                                                        realname = (string) e.Element("RealName"),
                                                        email = (string) e.Element("Email"),
                                                        phone = (string) e.Element("Phone"),
                                                        address = (string) e.Element("Address"),
                                                        link1 = (string) e.Element("Link1"),
                                                        link2 = (string) e.Element("Link2"),
                                                        link3 = (string) e.Element("Link3")

                                                    }
                                     ).ToList();
        }
...//code to get list value...

    }

But it has an error: Cannot resolve symbol "select"

Is there any better way to get data from profile.xml file???

Upvotes: 0

Views: 140

Answers (1)

Perevalov
Perevalov

Reputation: 348

There is an error in your from e in ... statement. XDocument.Load(targetPath).Root.Element("myXML") returns only one XML element for you. So you can't do linq query against a single object (don't mix with the collections containing only one object).

To get it working you need to change Element method to Elements:

from e in XDocument.Load(targetPath).Root.Elements("myXML")
select new profile
    {
        realname = (string) e.Element("RealName"),
        email = (string) e.Element("Email"),
        phone = (string) e.Element("Phone"),
        address = (string) e.Element("Address"),
        link1 = (string) e.Element("Link1"),
        link2 = (string) e.Element("Link2"),
        link3 = (string) e.Element("Link3")
    }

Update If you have only one myXML node in XML file (since it's a root node in your example) then you don't need linq query at all. Try to read data from the XML in a next way:

var prof =  XDocument.Load(targetPath).Root;
var p = new profile()
    {
        realname = prof.Element("RealName").Value,
        email = prof.Element("Email").Value,
        phone = prof.Element("Phone").Value,
        address = prof.Element("Address").Value,
        link1 = prof.Element("Link1").Value,
        link2 = prof.Element("Link2").Value,
        link3 = prof.Element("Link3").Value
    }

I've tested it with XML from your sample

Upvotes: 2

Related Questions