0

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文件中获取数据???

4

1 回答 1

2

您的from 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 对其进行了测试

于 2013-10-21T14:41:53.653 回答