使用linq读取xml文件

时间:2011-08-28 18:07:53

标签: c# xml linq

我有以下xml文件

<?xml version="1.0" encoding="utf-8"?> 
<Users>
    <User>
        <Name>John Smith</Name>
        <test>
            <Date>23.05.2011</Date>
            <points>33</points>
        </test>
        <test>
            <Date>22.06.2011</Date>
            <points>29</points>
        </test>
    </User>
</Users>

我想使用linq提取用户名为“John Smith”的测试日期和点数。

我将如何构建我的linq?

我做了以下工作,但是没有按照我的意愿工作:

XElement main = XElement.Load(@"users.xml");

string t = "John Smith";
var v = from user in main.Elements("User")
        where t == users.Element("Name").Value
        select users;

MessageBox.Show(v.First().Element("Date").Value.ToString()); 

4 个答案:

答案 0 :(得分:7)

我不确定您希望输出的格式是什么,但此示例代码应该获取日期和点数。这会将结果投影为匿名类型:

class Program
{
    static void Main(string[] args)
    {
        XElement main = XElement.Load(@"users.xml");

        var results = main.Descendants("User")
            .Descendants("Name")
            .Where(e => e.Value == "John Smith")
            .Select(e => e.Parent)
            .Descendants("test")
            .Select(e => new { date = e.Descendants("Date").FirstOrDefault().Value, points = e.Descendants("points").FirstOrDefault().Value });

        foreach (var result in results)
            Console.WriteLine("{0}, {1}", result.date, result.points);
        Console.ReadLine();
    }
}

输出是:

23.05.2011, 33
22.06.2011, 29

答案 1 :(得分:1)

XDocument main = XDocument.Load(@"users.xml"); 

string t = "John Smith"; 
var v = from user in main.Descendants("User") 
    where t == user.Element("Name").Value 
    select user; 

MessageBox.Show(v.First().Element("Date").Value.ToString()); 

应该做的伎俩。

答案 2 :(得分:1)

试试这个

class Program
{
    static void Main(string[] args)
    {
        XElement main = XElement.Parse(
@"<Users>
    <User>
        <Name>John Smith</Name>
        <test>
            <Date>23.05.2011</Date>
            <points>33</points>
        </test>
        <test>
            <Date>22.06.2011</Date>
            <points>29</points>
        </test>
    </User>
</Users>");

        var users =
           from m in main.Elements("User")
           where (string)m.Element("Name") == "John Smith"
           select (m.Descendants("test").Descendants("Date").FirstOrDefault().Value);
        foreach (var user in users)
            Console.WriteLine(user);
        Console.ReadLine();
    }
}

此致

答案 3 :(得分:0)

关于向John Smith添加另一个节点的另一个问题,这将是解决方案:

class Program
{
    static void Main(string[] args)
    {
        XElement main = XElement.Parse(
    @"<Users>
       <User>
            <Name>Alex</Name>
            <test>
                <Date>08.05.2011</Date>
                <points>4</points>
            </test>
        </User>
        <User>
            <Name>John Smith</Name>
            <test>
                <Date>23.05.2011</Date>
                <points>33</points>
            </test>
            <test>
                <Date>22.06.2011</Date>
                <points>29</points>
            </test>
        </User>
    </Users>");


    var users =
       from m in main.Elements("User")
       where (string)m.Element("Name") == "John Smith"
       select (m.Descendants("test").Descendants("Date").FirstOrDefault().Value);

    XElement Mercury = main.Elements("User").Where(p => (String)p.Element("Name") == "John Smith").FirstOrDefault();
    Mercury.Add(new XElement("test", new XElement("Date", "06.06.2011"), new XElement("points", "01")));

    foreach (var user in main.Elements())
        Console.WriteLine(user);

    Console.ReadLine();
}

}

给出下一个预期结果:

<User>
  <Name>Alex</Name>
  <test>
    <Date>08.05.2011</Date>
    <points>4</points>
  </test>
</User>
<User>
  <Name>John Smith</Name>
  <test>
    <Date>23.05.2011</Date>
    <points>33</points>
  </test>
  <test>
    <Date>22.06.2011</Date>
    <points>29</points>
  </test>
  <test>
    <Date>06.06.2011</Date>
    <points>01</points>
  </test>
</User>