0

次のコードがあります

XmlDocument docu = new XmlDocument();
        docu.Load(file);
XmlNodeList lst = docu.GetElementsByTagName("name");
                        foreach (XmlNode n in lst)
                        {
                            string text = n.InnerText;
    var types = doc.Element("program").Element("program-function").Element("function").Descendants("type").Where(x => x.Value == text).Select(c => c.Value).ToArray();
    }

私のxmlは次のとおりです

<program> 
  <program-function> 
    <function>
    <name>add</name> 
    <return-type>double</return-type> 
    <params> 
     <type>double</type> 
     <type-value>a</type-value> 
     <type>double</type> 
     <type-value>b</type-value> 
     <type>string</type> 
     <type-value>c</type-value> 
    </params> 
   <body> return a + b + c; </body> 
</function> 
  <function>
   <name>test</name> 
   <return-type>int</return-type> 
   <params> 
     <type>double</type> 
     <type-value>a</type-value> 
     <type>double</type> 
     <type-value>b</type-value> 
     </params> 
   <body> return a + b; </body> 
  </function> 
 </program-function> 
</program>

<type>それぞれの数を取得できる必要があります<name>

add の結果は 3 = types.count() = 3
test の結果は 2 =types.count() = 2

何かアドバイス?

編集:内部の各値を取得したい場合はtypes? すなわち。add、を含む必要がありabctest含む必要がaありますb。簡単に検索できるように配列に保存したい

4

2 に答える 2

1

Linq to Xml を使用するのはどうですか

 var xDoc = XDocument.Parse(xml);
var functions = xDoc.Descendants("function")
                .Select(f => new
                {
                    Name = f.Element("name").Value,
                    Types = f.Descendants("type").Select(t=>t.Value).ToList(),
                    //Types = f.Descendants("type").Count()
                    TypeValues = f.Descendants("type-value").Select(t=>t.Value).ToList()
                })
                .ToList();
于 2013-04-22T14:00:15.500 に答える
0

これを試して:

XDocument doc = XDocument.Load(your file);
var vals = doc.Element("program").Element("program-function").Elements("function");

var result = vals.Select(i => 
                     new { name = i.Element("name"), 
                           count = i.Elements("type").Count() }
于 2013-04-22T13:59:59.920 に答える