2015-10-22 136 views
-2

的索引值I有如下的非可变XML文件:C#/ XML:读取属性给定元素

<products> 
    <product> 
     <attributes> 
      <att name="Name" value="TOTO" /> 
      <att name="Surname" value="Toto" /> 
      <att name="Age" value="10" /> 
     </attributes> 
    </product> 
    <product> 
     <attributes> 
      <att name="Name" value="TATA" /> 
      <att name="Surname" value="Tata" /> 
      <att name="Age" value="20" /> 
     </attributes> 
    </product> 
    <product> 
     <attributes> 
      <att name="Name" value="TITI" /> 
      <att name="Surname" value="Titi" /> 
      <att name="Age" value="30" /> 
     </attributes> 
    </product> 
</products> 

使用C#,我需要提取字段的节点的值其值等于名称年龄,一个给定的索引处。很好的例子:输入查询将返回含有TITI的字符串,并含有30 另一个串。

就目前而言,我使用一个XmlDocument加载XML文件,并用的getElementsByTagName方法的XmlNodeList中获得所有属性;那么我可以显示这些元素,但是我不能只显示其中的两个,这取决于属性名称和索引。

充其量,我需要一个像myXmlNodeList.Node [“att”]。Attributes [“Name”]。AtIndex(x)

谁能帮助我?

+0

当然,你可能已经研究这个。 –

+0

我已经搜索过,但还没有找到任何真正接近我的答案。我发现了有关获取属性感谢节点,节点归功于属性等等。此外,许多方法使用XDocument或XmlDocument,我不认为混合这些类会是最佳的。另外,我无法使用外部库来执行此任务。 –

+0

的[如何阅读和分析在C#中的XML文件?(http://stackoverflow.com/questions/642293/how-do-i-read-and-parse-an-xml-file-in可能的复制-c) – Nasreddine

回答

0

询问这里之前尝试此

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Linq; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string xml = 
       "<products>" + 
        "<product>" + 
         "<attributes>" + 
          "<att name=\"Name\" value=\"TOTO\" />" + 
          "<att name=\"Surname\" value=\"Toto\" />" + 
          "<att name=\"Age\" value=\"10\" />" + 
         "</attributes>" + 
        "</product>" + 
        "<product>" + 
         "<attributes>" + 
          "<att name=\"Name\" value=\"TATA\" />" + 
          "<att name=\"Surname\" value=\"Tata\" />" + 
          "<att name=\"Age\" value=\"20\" />" + 
         "</attributes>" + 
        "</product>" + 
        "<product>" + 
         "<attributes>" + 
          "<att name=\"Name\" value=\"TITI\" />" + 
          "<att name=\"Surname\" value=\"Titi\" />" + 
          "<att name=\"Age\" value=\"30\" />" + 
         "</attributes>" + 
        "</product>" + 
       "</products>"; 

      XElement products = XElement.Parse(xml); 

      var results = products.Elements("product").Select(x => new 
      { 
       name = x.Descendants("att").Where(y => y.Attribute("name").Value == "Name").Select(z => z.Attribute("value").Value).FirstOrDefault(), 
       age = x.Descendants("att").Where(y => y.Attribute("name").Value == "Age").Select(z => (int)z.Attribute("value")).FirstOrDefault() 
      }).ToList(); 

      string name = results[2].name; 
      int age = results[2].age; 

     } 
    } 


} 
​ 
+0

这会引发Select方法的错误:“'System.Collections.Generic.IEnumerable '不包含'Select'的定义,也没有包含接受第一个参数的扩展方法'Select'可以找到类型'System.Collections.Generic.IEnumerable '“。尽管感谢您的帮助。 –

+0

已发布的代码适用于Net 4.0。确保你在我的代码顶部有所有的使用语句。我修改了原始XML,因为它没有匹配的标签。 – jdweng

+0

它的工作原理,谢谢。另外,我更正了我的问题中的XML文件,感谢您注意它!顺便说一下,我使用严格参数化的XPath表达式解决了我的问题,但我认为它可以以更简单的方式完成。 –