【问题标题】:To get first set of values in XML using c# Linq使用 c# Linq 获取 XML 中的第一组值
【发布时间】:2015-02-07 14:19:38
【问题描述】:

在下面的 XML 中,我需要获取 First NameList 的 Name 属性值,我尝试使用下面的代码,但我可以获取所有 name 属性值。我的预期输出类似于

预期输出:

Sample
NEw Test Application

    A1 : 565
    A2 : 354
    A3 : 2523
    A4 : 6756
    A5 : 7433

XML

<?xml version="1.0" encoding="utf-8" ?>
<Forum ID="1234">
  <ForumName>Sample</ForumName>
  <Description>NEw Test Application</Description>
  <NameList>
    <Name name="A1" value="565"></Name>
    <Name name="A2" value="354"></Name>
    <Name name="A3" value="2523"></Name>
    <Name name="A4" value="6756"></Name>
    <Name name="A5" value="7433"></Name>
  </NameList>
  <Threads>
    <Thread>
      <Id>123</Id>
      <StepName>543</StepName>
    <NameList>
      <Name name="A1" value="54|45"></Name>
      <Name name="A2" value="342|567"></Name>
      <Name name="B1" value="rtsd"></Name>
      <Name name="B3" value="432"></Name>
      <Name name="A4" value="9087"></Name>
    </NameList>
    </Thread>
    <Thread>
      <Id>125</Id>
      <StepName>541</StepName>
      <NameList>
        <Name name="A1" value="51|91"></Name>
        <Name name="A2" value="321|578"></Name>
        <Name name="B1" value="dgrw"></Name>
        <Name name="B3" value="415"></Name>
        <Name name="A4" value="876"></Name>
      </NameList>
    </Thread>
  </Threads>
</Forum>

C# 代码:

static void Main(string[] args)
        {
            string path = Directory.GetCurrentDirectory().Replace("\\bin\\Debug","");
            path = path + "//XML//XMLFile1.xml";
            XDocument xdoc = XDocument.Load(path);
            IEnumerable<XElement> elem = from a in xdoc.Descendants("Forum")
                            select a;
            foreach (var item in elem)
            {
                Console.WriteLine(item.Element("ForumName").Value);
                Console.WriteLine(item.Element("Description").Value + "\n");
                var firstNameList = from val in item.Descendants("Name")
                                    select new { AttributeName = val.Attribute("name").Value, AttributeVal = val.Attribute("value").Value };
                foreach (var NamelistItem in firstNameList)
                {
                    Console.WriteLine(NamelistItem.AttributeName + " : " + NamelistItem.AttributeVal);
                }
            }
            Console.ReadLine();
        }

【问题讨论】:

    标签: c# asp.net .net xml linq


    【解决方案1】:

    如果您只想在第一个NameList 节点中迭代,可以使用IEnumerable&lt;T&gt;.First() 方法:

    string path = Directory.GetCurrentDirectory().Replace("\\bin\\Debug","");
    path = path + "//XML//XMLFile1.xml";
    XDocument xdoc = XDocument.Load(path);
    
    var firstNameList= xdoc.Descendants("Forum").First()
        .Descendants("NameList").First()
        .Descendants("Name")
        .Select(val => new { AttributeName = val.Attribute("name").Value, AttributeVal = val.Attribute("value").Value })
        .ToList();
    

    【讨论】:

    • 感谢 Farhad,我尝试使用上面的代码,它工作正常并得到了预期的输出。
    猜你喜欢
    • 2019-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多