使用linq读取xml文件

本文关键字:文件 xml 读取 linq 使用 | 更新日期: 2023-09-27 18:06:47

我有以下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()); 

使用linq读取xml文件

我不确定您希望输出的格式,但这个示例代码应该得到日期和点。这将结果投影为匿名类型:

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

试试这个

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();
    }
}

问候

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()); 

应该做到这一点。

关于向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>