tags:

views:

1600

answers:

5

Hi I am kind of new to XML and .NET. I was wondering how I can parse the following file. I want to be able to get the XmlNode based on an ID I will provide. Example:

<Data>
<Question id="1">
<Answer>ddd</Answer>
</Question>
</Data>

I want to somehow do this:

XmlDocument myDoc = new XmlDocument();
XmlElement myElem = myDoc.GetElementById("1");
Response.Write("Answer: "+myElem.ChildNodes[0].InnerXML);

This should give me the output of: "Answer: ddd".

Any help is greatly appricated, thank you.

Also I would prefer not to use LINQ, as I would like to master the XML component of .NET first.

+2  A: 

The XML component of .Net was a piece of crap. Please don't subject yourself to it and just go LINQ to XML. By all considerations it's much easier to work with and runs faster.

Chris Lively
I am restricted to .NET 2.0 aswell...
@OliverS: then add that restriction to your question.
Chris Lively
I think his question was suited fine as he specified what he wants, no need to suggest something outside the scope of the question.
Pat
It's just the same wether with or without LINQ... only the LINQ model are easier to grok i.e. strongly typed and with method names that make more sense.Seriously, it isn't all that hard to upgrade to 3.5.. I've upgraded some projects without any code or configuration changes at all!
chakrit
I mean the model is still same hierachical node access model...
chakrit
@Pat: I posted this response due to OliverS's initially stated reasoning behind not going to LINQ. IMHO learning a garbage way of doing things simply because you want to master a reasonably dead technology isn't much of a reason. However, if he has a real restriction then my answer is worthless.
Chris Lively
That said, I have been on projects where using the 3.5 framework just wasn't an option due to client side restrictions. So I understand that aspect of it.
Chris Lively
+2  A: 

pseudo code: myDoc.selectSingleNode("//Question[@id=whatever]") (just top off my head)

Sachin
+2  A: 

Eyeballing it with no compilation:

XmlNode node = myDoc.SelectSingleNode("Data/Question[@id='1']/Answer"); Response.Write("Answer: " + node.InnerText);

Of course changing the 1 to be a parameter would be ideal if you want something variable.

Pat
Yeah this compiles fine, I was trying the same thing.var node = doc.SelectSingleNode("/Data/Question[@id='1']/Answer");Response.Write("Answer: " + node.InnerText);
Bryan Bailliache
+1  A: 

Prior to LINQ, I would have used SelectSingleNode with an XPath query. In the example above, the XPath would be something like "//Question[@id=1]/Answer". Now, I'd use LINQ because, as I understand it at least, you save overhead by not having to work with an XmlDocument object. By using an XElement and using a LINQ to XML extension method to query it for the node you want, you could do the whole thing in one or two lines of code and it will perform as good or better.

Here's an example one liner in LINQ using your xml snippet that doesn't even require instantiating an object to work with the xml:

string xml = "<Data><Question id=\"1\"><Answer>ddd</Answer></Question></Data>";
string answer = XElement.Parse(xml).Descendants("Question").
    Where(node => node.Attribute("id").Value == "1").
    Descendants("Answer").ElementAt(0).Value;
Rich
A: 

I think those pointing you at Linq and away from the XML components are correct. You may want to learn or use XPath for non .Net compatibility and Linq allows you to do that if you want. The XML components are not fun to work with.

amaca