Parsing XML within XML

  • Thread starter Thread starter Dinçer
  • Start date Start date
D

Dinçer

I need to parse an XML file and print the contents of it on an aspx page. My
XML file structure is like this:

<QueryResult>
<messageNo>1</messageNo>
<messageText />
<HERE>
<ExternalIntegrator><Entities>
<name>John</name>
<surname>Carpenter</surname>
<age>47</age>
</Entities></ExternalIntegrator>
</HERE>
<status>statusFound</status>
</QueryResult>

What I want to do is to get the data within "HERE" tags first. Then, I need
to parse this xml and get the name, surname etc. I mean, I need to parse the
xml document within the xml document.
I am trying to do it using XmlTextReader but when I use it second time (for
inner xml) I get the error PathTooLongException.

How can I parse this XML within XML and get the NAME value?



PS: What I basically do->
....
XmlTextReader reader = null;
reader = new XmlTextReader(UrlToXmlFile);
object oMyTag = reader.NameTable.Add("HERE");
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name.Equals(oMyTag))
{
return ReadXML2(reader.ReadString()); // here I want to do the same
thing for inner xml but get the error :(
}
....
 
hi
here is the code
use Dom Parsing

XmlDocument objDoc=new XmlDocument();
objDoc.Load("a.Xml");
XmlNode objNode;
objNode= objDoc.SelectSingleNode("//QueryResult/HERE");
MessageBox .Show(objNode.InnerXml );

XmlNode objSecNode;
objSecNode= objNode.SelectSingleNode("ExternalIntegrator/Entities");
MessageBox .Show(objSecNode.InnerXml );

MessageBox.Show(objSecNode.ChildNodes[0].InnerText);
MessageBox.Show(objSecNode.ChildNodes[1].InnerText);
MessageBox.Show(objSecNode.ChildNodes[2].InnerText);

objDoc=null;

regards
ansil
 
Thank you for the answer.
But I got the error when I use objDoc.SelectSingleNode(\\QueryResult\\xml):
'\QueryResult\xml' has an invalid token.

Where am I doing wrong?



Ansil MCAD said:
hi
here is the code
use Dom Parsing

XmlDocument objDoc=new XmlDocument();
objDoc.Load("a.Xml");
XmlNode objNode;
objNode= objDoc.SelectSingleNode("//QueryResult/HERE");
MessageBox .Show(objNode.InnerXml );

XmlNode objSecNode;
objSecNode= objNode.SelectSingleNode("ExternalIntegrator/Entities");
MessageBox .Show(objSecNode.InnerXml );

MessageBox.Show(objSecNode.ChildNodes[0].InnerText);
MessageBox.Show(objSecNode.ChildNodes[1].InnerText);
MessageBox.Show(objSecNode.ChildNodes[2].InnerText);

objDoc=null;

regards
ansil

Dinçer" <dincer"AT said:
I need to parse an XML file and print the contents of it on an aspx page. My
XML file structure is like this:

<QueryResult>
<messageNo>1</messageNo>
<messageText />
<HERE>
<ExternalIntegrator><Entities>
<name>John</name>
<surname>Carpenter</surname>
<age>47</age>
</Entities></ExternalIntegrator>
</HERE>
<status>statusFound</status>
</QueryResult>

What I want to do is to get the data within "HERE" tags first. Then, I need
to parse this xml and get the name, surname etc. I mean, I need to parse the
xml document within the xml document.
I am trying to do it using XmlTextReader but when I use it second time (for
inner xml) I get the error PathTooLongException.

How can I parse this XML within XML and get the NAME value?



PS: What I basically do->
....
XmlTextReader reader = null;
reader = new XmlTextReader(UrlToXmlFile);
object oMyTag = reader.NameTable.Add("HERE");
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name.Equals(oMyTag))
{
return ReadXML2(reader.ReadString()); // here I want to do the same
thing for inner xml but get the error :(
}
....
 
Oops.. Ignoe my question please..

\ vs. /

:-)


Dinçer said:
Thank you for the answer.
But I got the error when I use objDoc.SelectSingleNode(\\QueryResult\\xml):
'\QueryResult\xml' has an invalid token.

Where am I doing wrong?



Ansil MCAD said:
hi
here is the code
use Dom Parsing

XmlDocument objDoc=new XmlDocument();
objDoc.Load("a.Xml");
XmlNode objNode;
objNode= objDoc.SelectSingleNode("//QueryResult/HERE");
MessageBox .Show(objNode.InnerXml );

XmlNode objSecNode;
objSecNode= objNode.SelectSingleNode("ExternalIntegrator/Entities");
MessageBox .Show(objSecNode.InnerXml );

MessageBox.Show(objSecNode.ChildNodes[0].InnerText);
MessageBox.Show(objSecNode.ChildNodes[1].InnerText);
MessageBox.Show(objSecNode.ChildNodes[2].InnerText);

objDoc=null;

regards
ansil

Dinçer" <dincer"AT said:
I need to parse an XML file and print the contents of it on an aspx page. My
XML file structure is like this:

<QueryResult>
<messageNo>1</messageNo>
<messageText />
<HERE>
<ExternalIntegrator><Entities>
<name>John</name>
<surname>Carpenter</surname>
<age>47</age>
</Entities></ExternalIntegrator>
</HERE>
<status>statusFound</status>
</QueryResult>

What I want to do is to get the data within "HERE" tags first. Then, I need
to parse this xml and get the name, surname etc. I mean, I need to
parse
the
 
I tried it again but when executing this line:
"objSecNode.InnerXml "
it gets the error:
"Object reference not set to an instance of an object. "

Why could that happen?


Ansil MCAD said:
hi
here is the code
use Dom Parsing

XmlDocument objDoc=new XmlDocument();
objDoc.Load("a.Xml");
XmlNode objNode;
objNode= objDoc.SelectSingleNode("//QueryResult/HERE");
MessageBox .Show(objNode.InnerXml );

XmlNode objSecNode;
objSecNode= objNode.SelectSingleNode("ExternalIntegrator/Entities");
MessageBox .Show(objSecNode.InnerXml );

MessageBox.Show(objSecNode.ChildNodes[0].InnerText);
MessageBox.Show(objSecNode.ChildNodes[1].InnerText);
MessageBox.Show(objSecNode.ChildNodes[2].InnerText);

objDoc=null;

regards
ansil

Dinçer" <dincer"AT said:
I need to parse an XML file and print the contents of it on an aspx page. My
XML file structure is like this:

<QueryResult>
<messageNo>1</messageNo>
<messageText />
<HERE>
<ExternalIntegrator><Entities>
<name>John</name>
<surname>Carpenter</surname>
<age>47</age>
</Entities></ExternalIntegrator>
</HERE>
<status>statusFound</status>
</QueryResult>

What I want to do is to get the data within "HERE" tags first. Then, I need
to parse this xml and get the name, surname etc. I mean, I need to parse the
xml document within the xml document.
I am trying to do it using XmlTextReader but when I use it second time (for
inner xml) I get the error PathTooLongException.

How can I parse this XML within XML and get the NAME value?



PS: What I basically do->
....
XmlTextReader reader = null;
reader = new XmlTextReader(UrlToXmlFile);
object oMyTag = reader.NameTable.Add("HERE");
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name.Equals(oMyTag))
{
return ReadXML2(reader.ReadString()); // here I want to do the same
thing for inner xml but get the error :(
}
....
 
The queries Ansil specified are correct, perhaps your code doesn't
exactly match since there were syntax errors in the post. Are you sure
the second SelectSingleNode method call happens on the node found from
the previous call?

XmlDocument doc = new XmlDocument();
doc.Load(filename);
XmlNode hereNode = doc.SelectSingleNode("//QueryResult/HERE");
Console.WriteLine(hereNode.InnerXml);

XmlNode entityNode =
hereNode.SelectSingleNode("ExternalIntegrator/Entities");
Console.WriteLine(entityNode.InnerXml );

Console.WriteLine(entityNode.ChildNodes[0].InnerText);
Console.WriteLine(entityNode.ChildNodes[1].InnerText);
Console.WriteLine(entityNode.ChildNodes[2].InnerText);
 
Actually, I go to a URL first which returns me the main XML.
Main XML:
<?xml version="1.0" encoding="windows-1254" ?>
- <QueryResult>
<messageNo>1</messageNo>
<messageText />

<xml><ExternalIntegrator><Entities><Entity><Fields><NAME>JOHN</NAME><MIDDLE_
NAME
/> said:
<status>statusFound</status>
</QueryResult>

So, I use:

objDoc = new XmlDocument();
objDoc.Load(new XmlTextReader(myURL));
XmlNode objNode;
objNode = objDoc.SelectSingleNode("//QueryResult/xml");
XmlNode objSecNode;
objSecNode =
objNode.SelectSingleNode("ExternalIntegrator/Entities/Entity/Fields");
Response.Write(objSecNode.InnerXml);
Response.Write("\n<b>"+objSecNode.ChildNodes[0].InnerText);

However, I got the error:
"Object reference not set to an instance of an object. "

Do I miss anything?


Scott Allen said:
The queries Ansil specified are correct, perhaps your code doesn't
exactly match since there were syntax errors in the post. Are you sure
the second SelectSingleNode method call happens on the node found from
the previous call?

XmlDocument doc = new XmlDocument();
doc.Load(filename);
XmlNode hereNode = doc.SelectSingleNode("//QueryResult/HERE");
Console.WriteLine(hereNode.InnerXml);

XmlNode entityNode =
hereNode.SelectSingleNode("ExternalIntegrator/Entities");
Console.WriteLine(entityNode.InnerXml );

Console.WriteLine(entityNode.ChildNodes[0].InnerText);
Console.WriteLine(entityNode.ChildNodes[1].InnerText);
Console.WriteLine(entityNode.ChildNodes[2].InnerText);

--
Scott
http://www.OdeToCode.com/


I tried it again but when executing this line:
"objSecNode.InnerXml "
it gets the error:
"Object reference not set to an instance of an object. "

Why could that happen?
 
BTW, innerXML value of objDoc.SelectSingleNode("//QueryResult/xml")
returns:
&lt;ExternalIntegrator&gt;&lt;Entities&gt;&lt;Entity
instead of
<ExternalIntegrator><Entities><Entity...

Could that be the problem?


Scott Allen said:
The queries Ansil specified are correct, perhaps your code doesn't
exactly match since there were syntax errors in the post. Are you sure
the second SelectSingleNode method call happens on the node found from
the previous call?

XmlDocument doc = new XmlDocument();
doc.Load(filename);
XmlNode hereNode = doc.SelectSingleNode("//QueryResult/HERE");
Console.WriteLine(hereNode.InnerXml);

XmlNode entityNode =
hereNode.SelectSingleNode("ExternalIntegrator/Entities");
Console.WriteLine(entityNode.InnerXml );

Console.WriteLine(entityNode.ChildNodes[0].InnerText);
Console.WriteLine(entityNode.ChildNodes[1].InnerText);
Console.WriteLine(entityNode.ChildNodes[2].InnerText);

--
Scott
http://www.OdeToCode.com/


I tried it again but when executing this line:
"objSecNode.InnerXml "
it gets the error:
"Object reference not set to an instance of an object. "

Why could that happen?
 
hi
this following code is working fine in my PC

XmlDocument objDoc=new XmlDocument();
objDoc.Load("a.Xml");
XmlNode objNode;
objNode= objDoc.SelectSingleNode("//QueryResult/xml");
MessageBox .Show(objNode.InnerXml );

XmlNode objSecNode;
objSecNode=
objNode.SelectSingleNode("ExternalIntegrator/Entities/Entity/Fields");
MessageBox .Show(objSecNode.InnerXml );
MessageBox.Show(objSecNode.ChildNodes[0].InnerText);
MessageBox.Show(objSecNode.ChildNodes[1].InnerText);
MessageBox.Show(objSecNode.ChildNodes[2].InnerText);

objDoc=null;

take care that the xpath expression in the SelectSingleNode mehtod is case
sensitive.i think thats the problem with u

regards
ansil

Dinçer" <dincer"AT said:
Actually, I go to a URL first which returns me the main XML.
Main XML:
<?xml version="1.0" encoding="windows-1254" ?>
- <QueryResult>
<messageNo>1</messageNo>
<messageText />

<xml><ExternalIntegrator><Entities><Entity><Fields><NAME>JOHN</NAME><MIDDLE_
NAME
/> said:
<status>statusFound</status>
</QueryResult>

So, I use:

objDoc = new XmlDocument();
objDoc.Load(new XmlTextReader(myURL));
XmlNode objNode;
objNode = objDoc.SelectSingleNode("//QueryResult/xml");
XmlNode objSecNode;
objSecNode =
objNode.SelectSingleNode("ExternalIntegrator/Entities/Entity/Fields");
Response.Write(objSecNode.InnerXml);
Response.Write("\n<b>"+objSecNode.ChildNodes[0].InnerText);

However, I got the error:
"Object reference not set to an instance of an object. "

Do I miss anything?


Scott Allen said:
The queries Ansil specified are correct, perhaps your code doesn't
exactly match since there were syntax errors in the post. Are you sure
the second SelectSingleNode method call happens on the node found from
the previous call?

XmlDocument doc = new XmlDocument();
doc.Load(filename);
XmlNode hereNode = doc.SelectSingleNode("//QueryResult/HERE");
Console.WriteLine(hereNode.InnerXml);

XmlNode entityNode =
hereNode.SelectSingleNode("ExternalIntegrator/Entities");
Console.WriteLine(entityNode.InnerXml );

Console.WriteLine(entityNode.ChildNodes[0].InnerText);
Console.WriteLine(entityNode.ChildNodes[1].InnerText);
Console.WriteLine(entityNode.ChildNodes[2].InnerText);

--
Scott
http://www.OdeToCode.com/


I tried it again but when executing this line:
"objSecNode.InnerXml "
it gets the error:
"Object reference not set to an instance of an object. "

Why could that happen?
 
Back
Top