8

I have a XML file like this and I want to read the ID, shortname, Name node value.

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
<AccountingUnitList xmlns="http://www.google.com">
<AccountingUnit>
<ID>17406</ID> 
<ShortName>test</ShortName> 
<Name>test</Name> 
</AccountingUnit>
<AccountingUnit>
<ID>18006</ID> 
<ShortName>ANOTHERtest</ShortName> 
<Name>Anothertest</Name> 
</AccountingUnit>
<AccountingUnit>
<ID>18046</ID> 
<ShortName>RKU</ShortName>
<Name>hospital</Name> 
</AccountingUnit>
<AccountingUnit>
<ID>18047</ID> 
<ShortName>MNU</ShortName> 
<Name>MNU</Name> 
</AccountingUnit>
</AccountingUnitList>

what is the best way to read the Node element recursively?

This is how I am trying to read the Node value:

var accountingunit = ( 
                from e in XDocument.Parse(textresult).Root.Elements("AccountingUnit")
                select new node
                {
                     idvalue = (string)e.Element("ID"),
                     shortname =(string)e.Element("ShortName"),
                     name = (string)e.Element("Name"),

                });

            foreach(var unit in accountingunit)
            {
               Console.WriteLine("ID"+ unit.idvalue + unit.name + unit.shortname);
            }

Here is the node consructor:

public class node
{
    public string idvalue { get; set; }
    public string shortname { get; set; }
    public string name { get; set; }
}
M. Haché
  • 354
  • 6
  • 17
user1514428
  • 195
  • 1
  • 7
  • 3
    Show what you tried please. – Sybren Dec 02 '14 at 11:15
  • With some googling, you find the answers. http://stackoverflow.com/questions/14504057/xdocument-get-xml-element-by-the-value-of-its-name-attribute http://stackoverflow.com/questions/8460464/finding-element-in-xdocument http://stackoverflow.com/questions/566167/query-an-xdocument-for-elements-by-name-at-any-depth https://social.msdn.microsoft.com/forums/vstudio/en-US/3d457c3b-292c-49e1-9fd4-9b6a950f9010/how-to-get-tag-name-of-xml-by-using-xdocument – James Shaw Dec 02 '14 at 13:08
  • Add the word recursive and you get more! http://www.codearsenal.net/2012/12/csharp-reading-xml-document-recursively.html http://forums.asp.net/t/1535653.aspx?Traversing+XML+file+using+recursion http://ehikioya.com/get-xml-document-nodes-recursively/ – James Shaw Dec 02 '14 at 13:10

1 Answers1

17

You have an xml namespace in your document.All the child elements of AccountingUnitList inherits the namespace so you need to specify it via element name:

XNamespace ns = "http://www.google.com";

var accountingunit = ( 
            from e in XDocument.Parse(textresult).Elements(ns + "AccountingUnit")
            select new node
            {
                 idvalue = (string)e.Element(ns + "ID"),
                 shortname =(string)e.Element(ns + "ShortName"),
                 name = (string)e.Element(ns + "Name"),

            });
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • @user1514428 try removing the `.Root`. Since you are using `XElement.Parse` root is already `AccountingUnitList` – Selman Genç Dec 02 '14 at 12:51
  • @user1514428 It seems I miss the namespace here: `.Elements(ns + "AccountingUnit")` try again – Selman Genç Dec 02 '14 at 13:05
  • Thank you very much it worked but i have one problem. Actually the xml is result of a webservice request of all Accountingunit but if i request for one particular accountingunit id the the XML is like this 10000 TEST KH 01 ORBIS TEST-Krankenhaus 01 1 how can do it ?. – user1514428 Dec 02 '14 at 13:17
  • if the xml is like this ; 10000 TEST KH 01 ORBIS TEST-Krankenhaus 01 1 what should i use in .Elements(ns + ? ) . thank you. – user1514428 Dec 03 '14 at 13:25
  • @user1514428 It seems your problem requires a new question, please only use comments to ask for further details. Also, if this question helped you, please green mark it. – M. Haché Sep 20 '16 at 14:58