-1

my XML File is something like

<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>

Is there a way i can get the value of "buchungsart" (1100) using the xlink:href urn? This is what i tried so far:

    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);
    }

I want to achieve that i read out the AX_Flurstueck Node and get all the values which are linked by the "istGebucht xlink:href" node. I hope someone can help or give a hint, i did not find anything to this topic.

1 Answers1

0

Using xml linq I grouped all the hrefs to there corresponding elements

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());     

        }
    }

}
jdweng
  • 33,250
  • 2
  • 15
  • 20