3

以下は私のxmlドキュメントの構造です。<attribute name="a">最初にすべてのノードの値を取得してから、指定された値と比較したいと思います。<attribute name="a">ただし、c#でxmlselectnodesを使用して各ノードを見つける方法はありません。Google検索では、実用的なソリューションは表示されません。

<nodes>     
 <node name = "node1">      
  <attribute name="a">This is node1 a</attribute>
  <attribute name="b">This is node1 b</attribute>
 </node>
 <node name = "node2">      
  <attribute name="a">This is node2 a</attribute>
  <attribute name="b">This is node2 b</attribute>
 </node>
 ...
</nodes>     
4

4 に答える 4

4

次のように、LinqtoXMLを使用できます。

string xml = "<nodes>...";

var results = from node in XDocument.Parse(xml).Descendants()
          where node.Name == "attribute"
          select node.Value;

その後、必要に応じて結果をループできます。

ここにもLinqtoXMLの概要があります。

于 2012-01-17T16:29:51.607 に答える
3

LinqtoXMLを使用します。

XElement xml = XElement.Load("test.xml");
var myNodes = xml.Descendants("attribute")
                 .Where(x => x.Attribute("name").Value == "a");

ノードの代わりに値を取得するには:

var myValues = xml.Descendants("attribute")
                  .Where(x => x.Attribute("name").Value == "a")
                  .Select(x => x.Value);
于 2012-01-17T16:28:53.283 に答える
2

質問のXMLマークアップがドキュメント全体を表していると仮定すると、次のことができます。

XmlNodeList attrElements
    = yourDocument.SelectNodes("/nodes/node/attribute[@name='a']");
于 2012-01-17T16:29:27.153 に答える
1

System.Xml.XmlDocumentxml解析にクラス を使用するのが好きです。

XmlDocument doc = new XmlDocument();
doc.load("myfilename.xml");
XmlNode node = doc.SelectSingleNode("\\attribute[name='a']")

xpath文字列が正しく取得されていることを確認するために、いくつかのXPathリファレンスを確認する必要がありますhttp://msdn.microsoft.com/en-us/library/ms256086.aspx

于 2012-01-17T16:32:47.400 に答える