【问题标题】:read xml file using linq使用 linq 读取 xml 文件
【发布时间】:2011-08-28 18:07:52
【问题描述】:

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

【问题讨论】:

  • 您的代码不应该编译。您正在使用users,您应该使用user。而且您的 t 初始化中有错字。

标签: c# xml linq


【解决方案1】:

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

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

【讨论】:

  • GREAT ..谢谢我如何在 xml 文件中插入一个新的“在这个例子中是第三个” 节点并指向该用户(John Smith)?
  • @JohnD 这非常有帮助。谢谢。
【解决方案2】:

试试这个

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

    应该可以解决问题。

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-15
        • 2011-03-17
        相关资源
        最近更新 更多