views:

47

answers:

4

I have the a full XML file in an XDocument variable which I get from some API like this

using (var reader = XmlReader.Create("website"))
        {
            doc = XDocument.Load(reader);
        }

I need to get the structure of the XML and navigate through its nodes, but through the XDocument variable, I only get the whole document in one node and can not extract each node by itself. So any solution or shall I use another way?

+2  A: 

http://www.hookedonlinq.com/LINQtoXML5MinuteOverview.ashx is a good article on building it up

you can also use linq to query it

for instance

var loaded = XDocument.Load("sdaf");
var q = from c in loaded.Descendants("contact")
        where (int)c.Attribute("contactId") < 4
        select (string)c.Element("firstName") + “ “ +
      (string)c.Element("lastName");

taken off the page i linked to above.

The XDocument and XElement objects kick ass in my opinion. If you dont like them then go learn xpath and xslt.

John Nicholas
A: 
string xml = reader.ReadToEnd();

XmlDocument thisXmlDoc = new XmlDocument();
thisXmlDoc.LoadXml(xml); // In your case DOC

reader.Close();

XPathNavigator thisNavigator = thisXmlDoc.CreateNavigator();
XPathNodeIterator dossierNodes = thisNavigator.Select("Nodename/node");

List<Dossier> thisList = GetDossiers(dossierNodes);
thisDossierList = thisList.OrderBy(c => c.something).ToList();

he is using XDocument objects, not XMLDocument objects. – John Nicholas 52 secs ago

Damn, you're right. Sorry! Only tried to help...

Younes
he is using XDocument objects, not XMLDocument objects.
John Nicholas
I see, haven't looked good enough there...
Younes
nothing wrong with XMLDOcument, it just that it works best with a XPath and XSLT approach. .net only implements xpath 1.0 btw - which really effects what you can do in xslt. Hah i like the way you did that btw. Still prefer linq2xml though.
John Nicholas
Thx for your comments :)!
Younes
+1  A: 

To obtain the immediate child nodes of your XDocument you can try

    using (var reader = XmlReader.Create("website"))
    {
        var doc = XDocument.Load(reader);
        var childElements = doc.Elements();
    }

Then do further processing such as childElements.Descendants("name").Single().Value.

Noel Abrahams
I tried the first part but it didn't work, not sure about the second one.
Ahmad Farid
A: 
XmlDocument xdoc;
xdoc = new XmlDocument();
xdoc.Load(XmlReader.Create("weblink"));

The XDocument is not possible to be analyzed and extract its XML values which is possible in XmlDocument

Ahmad Farid
@Ahmad, I think what you need is XDocument.Load(). Which is a STATIC method. I've updated my answer to reflect this.
Noel Abrahams
Your method should work as well I guess. But anyway, I don't have to use XDcocument; XmlDocument is simpler so I used it. Thanks for your participation though :)
Ahmad Farid
I disagree, I find XDocument and XElment far easaier to use as everything is strongly typed. This is more true when you get it driven by an xsd file. when you have time you shuold check it out.
John Nicholas