【问题标题】:C# Get Value of Xlink:href XML NodeC# 获取 Xlink:href XML 节点的值
【发布时间】:2023-03-15 06:47:01
【问题描述】:

我的 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 urn 获取“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”节点链接的所有值。我希望有人可以提供帮助或提示,我没有找到任何关于这个主题的内容。

【问题讨论】:

    标签: c# xml href xlink


    【解决方案1】:

    使用 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());     
    
            }
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2013-08-12
      • 1970-01-01
      • 1970-01-01
      • 2013-03-19
      • 2019-12-03
      • 1970-01-01
      • 1970-01-01
      • 2016-10-09
      • 2012-07-05
      相关资源
      最近更新 更多