c#
  • xml
  • xpath
  • 2013-04-11 8 views 6 likes 
    6

    特定の名前の属性を含むすべてのノードを選択する必要があります。特定の属性を含むxmlノードをすべて選択してください。

    これは私の現在のアプローチではありません。

    public List<string> RetrieveValuesForAttribute(string attributeName) 
    { 
        var list = new List<string>(); 
    
        string xpath = "//*[@Name='" + attributeName + "']"; 
        XmlNodeList xmlNodeList = document.SelectNodes(xpath); 
    
        foreach (XmlNode xmlNode in xmlNodeList) 
        { 
         list.Add(xmlNode.Attributes[attributeName].InnerText); 
        } 
    
        return list; 
    } 
    

    私は、メソッドのパラメータattributeNameに指定された名前の属性を含むすべてのノードを選択し、値に変数listを追加しよう。

    例:

    このメソッドの呼び出し:

    List<string> result = RetrieveValuesForAttribute("itemSelectedHandler"); 
    

    は、文字列を含むリストを返す必要があります。これは、XMLファイルです

    を "OnSelectedRelatedContactChanged":

    <GroupBoxWrapper id="gbRelatedContacts" text="Related Contacts"> 
        <TabIndex>0</TabIndex> 
        <TabStop>false</TabStop> 
        <PanelWrapper id="pnlRelatedContactsView" width="1350"> 
        <TabIndex>0</TabIndex> 
        <TabStop>false</TabStop> 
        <ListViewWrapper id="lvRelatedContacts" itemSelectedHandler="OnSelectedRelatedContactChanged" itemDoubleClickHandler="OnRelatedContactDoubleClick"> 
         <TabIndex>0</TabIndex> 
         <TabStop>true</TabStop> 
         <ListViewColumns> 
         <Column title="Name" mapNode="Contact\Name" /> 
         <Column title="Lastname" mapNode="Contact\Lastname" /> 
         </ListViewColumns> 
        </ListViewWrapper> 
        </PanelWrapper> 
    </GroupBoxWrapper> 
    

    さらにキューstions: これをLINQで解決する方が良いでしょうか?

    解決策1:ありがとう、YWM

    public List<string> RetrieveValuesForAttribute(string attributeName) 
    { 
        var list = new List<string>(); 
    
        string xpath = @"//*[@" + attributeName + "]"; 
        XmlNodeList xmlNodeList = document.SelectNodes(xpath); 
    
        foreach (XmlNode xmlNode in xmlNodeList) 
        { 
         list.Add(xmlNode.Attributes[attributeName].InnerText); 
        } 
    
        return list; 
    } 
    

    解決策2:ありがとう、ジョンスキート

    public List<string> RetrieveValuesForAttribute(string attributeName) 
    { 
        //document is an XDocument 
        return document.Descendants() 
            .Attributes(attributeName) 
            .Select(x => x.Value) 
            .ToList(); 
    } 
    

    LINQのXMLソリューションには、はるかにエレガントな私には見えます。

    +1

    私は間違いなくこのためにXMLにLINQを使用します。代わりに 'document'を' XDocument'にできますか? –

    +0

    はい、私はXMLにLINQで試してみることができます – Joel

    答えて

    8

    あなたはこのためにXMLにLINQを使用することができれば、それは全く些細なことになります。

    // Note that there's an implicit conversion from string to XName, 
    // but this would let you specify a namespaced version if you want. 
    public List<string> RetrieveValuesForAttribute(XName attributeName) 
    { 
        // Assume document is an XDocument 
        return document.Descendants() 
            .Attributes(attributeName) 
            .Select(x => x.Value) 
            .ToList(); 
    } 
    
    4

    あなたが探しているXPathは、あなたのオリジナルのXPathを探していたが何をやっていた

    "//*[@" + attributeName + "]" 
    

    する必要がありますNameの値を持つすべての要素についてattributeName

    これは、属性がattrの要素を検索しますibuteName

    //*[@title] 
    

    はC#構文について

    +0

    ありがとう、あなたのソリューションのためにうまくいきます。しかし、私はLINQ to XMLのアプローチがより好きです。 – Joel

    1

    イムわからない列要素を返しますが、私は、XPath vlaueが間違っていると思います。 「// * [@ itemSelectedHandler]」を入力してください。 C#

    string xpath = "//*[@" + attributeName + "]"; 
    
    関連する問題