Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an XML Document that I inherited to run a report on. A sample of the XML document is below. I am trying to get the value ("pp4.PowerProfileID") out of the profile node. While iterating through the child nodes of "day" I used the code

foreach (XmlNode profileNode in dayNode.SelectNodes("profile"))
{
    string profileID = profileNode.InnerText;
    ....
}


profileID receives the value "pp4.PowerProfileID.01" on the second iteration of the loop. This was not the value I initially expected.

XML
<sum>
  <day d="1" >
    <profile mcount="1"  pmins="1440">pp1.PowerProfileID</profile>
    <profile mcount="0"  pmins="0" >pp4.PowerProfileID
      <hour minutes="0" >.01</hour>
    </profile>
  </day>
</sum>


I have read the MSDN documentation and understand that .InnerText concatenates the value of the node and all child nodes.

So my questions are:
Why would the DOM it be designed to concatenate values for .InnerText? I am not arguing what it does, but why it would work the way it does. Can somebody present an example of where this would be helpful?

In my xml example "pp4.PowerProfileID" is the ID of the object. Should that have been made its own node?

Ultimately I want the ID/text of the "profile" node and not the text inside the child "hour" node. Can anyone recommend a better way than using the .OuterXML value with a regex to get the text out of it?

Thanks for any thoughts!

Hogan
Posted
Updated 19-Jan-11 7:45am
v2
Comments
Nish Nishant 19-Jan-11 11:49am    
Your code is incomplete/broken, probably a copy/paste issue. Can you fix that?

What you may be looking for may be InnerXml (which will return what you expect).

[Edit]
---------

Based on our discussion via comments, here's something you could try:

C#
var node = profileNode.ChildNodes.Cast<XmlNode>().Where(n => n.Name == "#text").FirstOrDefault();
if (node != null)
{
    string s = node.Value;
}
 
Share this answer
 
v2
Comments
snorkie 19-Jan-11 13:47pm    
Thanks, but innerXML will return "pp4.PowerProfileID<hour minutes="0">.01" when I am only after "pp4.PowerProfileID"
Nish Nishant 19-Jan-11 14:08pm    
Ok, that's not what InnerXml returns, since it returns the xml tags too. But I am confused now. InnerText will give you what you want, but you said that was not enough and that's why everyone here suggested that you use InnerXml.
snorkie 19-Jan-11 14:34pm    
Sorry, I got that backwards. For an XmlNode of "profile"
.InnerText returns "pp4.PowerProfileID.01".
.InnerXml returns "pp4.PowerProfileID<hour minutes="0">.01"
.value returns <null>

What i wanted is "pp4.PowerProfileID"

I don't mean to state that the .NET framework is wrong. I'm just surprised by the results. Trying to figure out if the XML was designed wrong, or I'm just trying to get at the answer the wrong way.
Nish Nishant 19-Jan-11 14:36pm    
That's weird. I tried it out in VS 2010,

InnerText is pp4.PowerProfileID.01

InnerXml is pp4.PowerProfileID<hour minutes="0">.01</hour>
Sergey Alexandrovich Kryukov 19-Jan-11 17:42pm    
Nothing weird. Everything is exactly standard.
All correct. InnerText can be very useful. Effectively, this is pure text cleaned from all XML Tags. Imagine XML representing XHTML. The InnerText if its Document node will give you pure text representation, pretty much as if you copied the rendered text and pasted in some text editor.

What's wrong with that?

Also important, that this should be done with any child node. InnerXml will give you well-formed XML that you could save as a separate document, and InnerText will immediately give you a pure-text representation of this inner XML.

Bad design of DOM implementation of .NET? I don't think so. First of all, it conforms to standards, well implemented. Of course, the performance is not the best, but for performance goals you can use XmlReader/XmlWriter.

You did not explain how you want to process your XML. Anyway, you have comprehensively parsed data in your instance XmlDocument -- learn to use it. You can get some help, but only if your explain your ultimate goals.
 
Share this answer
 
v2
Comments
Nish Nishant 19-Jan-11 12:14pm    
Detailed answer. Voted 5, and proposed as answer.
snorkie 19-Jan-11 13:32pm    
I did not mean to suggest that the DOM implementation of .NET was bad, just the design of the XML document I am working with.
Sergey Alexandrovich Kryukov 19-Jan-11 18:44pm    
Oh, I understand, may be.
Thanks for this note.
Espen Harlinn 19-Jan-11 15:22pm    
Good answer
If you're ultimately looking for the value of the hour element, just get the InnerText for the child node instead of the node you're getting it for now.
 
Share this answer
 
Comments
snorkie 19-Jan-11 13:47pm    
I'm actually looking for the text from hour's parent node.
Sergey Alexandrovich Kryukov 19-Jan-11 17:41pm    
The question was: what value you want to obtain? If you're looking for text property, it can be either element text or some attribute value. What do you need?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900