C#获取Xlink:href XML节点的值

时间:2018-08-18 13:01:13

标签: c# xml href xlink

我的XML文件就像

<AX_Flurstueck>
        <istGebucht xlink:href="urn:adv:oid:DEBBAL0600000Y9V"/>
</AX_Flurstueck>
<AX_Buchungsstelle gml:id="DEBBAL0600000Y9V">
    <gml:identifier codeSpace = "http://www.adv-online.de/">urn:adv:oid:DEBBAL0600000Y9V</gml:identifier>
        <buchungsart>1100</buchungsart>
</AX_Buchungsstelle>

有没有一种方法可以使用xlink:href缸获取“ buchungsart”(1100)的值?这是我到目前为止尝试过的:

    XmlDocument xmlDoc = new XmlDocument();
    string str = @"XML-File-Direction";
    xmlDoc.Load(str);
    XmlNodeList flst_list = xmlDoc.SelectNodes("//ns2:AX_Flurstueck", nsmgr);
    foreach (XmlNode flstnodeExt in flst_list)
    {
        string hrefXmlDocument = 
        xmlDoc.DocumentElement.Attributes["xlink:href"].Value;
        Console.WriteLine(hrefXmlDocument);
    }

我想实现读取AX_Flurstueck节点并获取所有由“ istGebucht xlink:href”节点链接的值。我希望有人可以提供帮助或提供提示,但我对此主题没有任何帮助。

1 个答案:

答案 0 :(得分:0)

使用xml linq,我将所有href分组到了相应的元素

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


namespace ConsoleApplication1
{
    public class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        public static void Main()
        {
            XDocument doc = XDocument.Load(FILENAME);

            Dictionary<string,string> hrefs = doc.Descendants().Select(x => x.Attributes().Where(y => y.Name.LocalName == "href")).SelectMany(x => x)
                .GroupBy(x => (string)x, y => y.Name.NamespaceName)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());

            Dictionary<string, List<XElement>> dict = doc.Descendants().Where(x => hrefs.ContainsKey((string)x))
                .GroupBy(x => hrefs[(string)x], y => y)
                .ToDictionary(x => x.Key, y => y.ToList());     

        }
    }

}