Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple xml parsing - help needed 2

Status
Not open for further replies.

DotNetGnat

Programmer
Mar 10, 2005
5,548
IN
Guys,

My xml document looks like this
Code:
<worksheet>
	[blue]<metadata>
		<generator>blah</generator>
		<userData>
			<title/>
			<description/>
			<author>John</author>
		</userData>
		<identityInfo>
			<revision>34</revision>
			<documentID>sdfds</documentID>
		</identityInfo>
	</metadata>[/blue]

	[green]<regions>

		<region>

			<text>
				<p style="Yes">18.5<tab/>Design Example</p>
			</text>

		</region>

		<region>

			<text>
				<p style="No">some text</p>
			</text>

		</region>

		<region>

		<text>
			<p style="Yes">18.5.1<tab/>Structure Example</p>
		</text>

		</region>
	</regions>[/green]
</worksheet>
I want to get the result as

18.5 Design Example
18.5.1 Structure Example

that is i am only wanting to get the values of the <p> tag if the style attribute inside it is "yes"

Here is the function i have so far...

Code:
 private string ProcessXml(XmlTextReader xmlReader) 
   {
   StringBuilder temp = new StringBuilder();
 
   while ( xmlReader.Read() ) 
   {
     if (xmlReader.NodeType == XmlNodeType.Element)
    {
		if (xmlReader.Name=="p")
		{
			temp.Append(xmlReader.Name + "<br>");
		}
     
    }
   
   return temp.ToString();
   
 }

as you see i am able to get the name but when i try xmlReader.Value, i get empty values...

can someone please assist me with this.

thanks

-DNG
 
>but when i try xmlReader.Value, i get empty values...
If you consult the documentation, .Value on the element node would be in the category of "All other node types" and will return empty.
For element node, should use ReadString() for its text node content.
otherwise, .ReadInnerXml() as mentioned in the 1st ref., but it risks to give you more than you want in general.
 
wonderful tsuji...

i fixed the code...ReadInnerXml() did the job...

but i have one final question...at one point in my code...in order to check the value of the attribute i am doing this...

string attri = xmlReader.GetAttribute("style");
if (attri == "yes")

the above works fine...no problems there...

but if i want to just compare a substring of the attribute with a value, it is complaining...

i mean if i do something like this...

string attri = xmlReader.GetAttribute("style");
if (attri.substring(1,3) == "yes")

that makes me think that it is a not a string...so i tried ToString() and other convert things but did not work...

any suggestions...

-DNG
 
c# is case-sensitive (Substring()). Or is it not c#?
 
it is c#...

it gives me weird error when i use the Substring()

here is the error...

Code:
A general exception occurred: System.ArgumentOutOfRangeException: Index and length must refer to a location within the string. Parameter name: length at System.String.Substring(Int32 startIndex, Int32 length) at ASP.XmlDisplay.ProcessXml(XmlTextReader xmlReader)

this is my code

Code:
	switch(xmlReader.Name)
		{
case "p":
string attri = xmlReader.GetAttribute("style");
//if (attri.Length > 15)
//{
if (attri.Substring(1,14) == "blahblahblahbl")
{
temp.Append(xmlReader.ReadInnerXml() + "<br>");
}
//}
break;
					
default:
break;
}

seeing the error...i tried checking the length (commented in the code) but when i did that i am getting no results...

thanks

-DNG
 
That I think is a more sensible error message: that means Substring() and attrib are functioning. The problem I suspect is this. If you get attrib after the line with using the method ReadInnerXml, the position of the reader is on the next tag (refer to the 1st ref I quoted). This is the reason, I guess, of the abberration. Could you look into it?
 
yes...i made a simple mistake...thanks guys...

-DNG
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top