从xml文件获取数据时出错

时间:2013-10-21 14:22:54

标签: c# xml linq

我的网络文件夹中有profile.xml个文件:

<?xml version="1.0" encoding="utf-8"?>
<myXML>
  <RealName>Nguyen Van A</RealName>
  <Email>vyclarks@gmail.com</Email>
  <Phone>2165421</Phone>
  <Address>Ho Chi Minh</Address>
  <Link1>dtvt</Link1>
  <Link2></Link2>
  <Link3></Link3>
</myXML>

根据建议,我通过以下代码从该文件中获取数据:

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...

    }

但它有一个错误:Cannot resolve symbol "select"

有没有更好的方法从profile.xml文件???

获取数据

1 个答案:

答案 0 :(得分:2)

来自e in ... 语句中存在错误。 XDocument.Load(targetPath).Root.Element(“myXML”)只为您返回一个XML元素。所以你不能对单个对象进行linq查询(不要与只包含一个对象的集合混合)。

要使其正常工作,您需要将Element方法更改为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")
    }

<强>更新 如果XML文件中只有一个myXML节点(因为它是示例中的根节点),那么根本不需要linq查询。尝试以下一种方式从XML读取数据:

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
    }

我已经使用您的示例中的XML进行了测试