XMLDocument class parsing

G

Guest

I try to parse a XML document containg some references using XmlDocument
Class' method GetElementbyTagName. It just give the content between starting
tagName and ending tagName but not all refernces used between starting
tagName and ending tagName. Is there any way to achive this?
 
J

Jon Skeet [C# MVP]

<"=?Utf-8?B?T21rYXIgU2luZ2g=?=" <Omkar
I try to parse a XML document containg some references using XmlDocument
Class' method GetElementbyTagName. It just give the content between starting
tagName and ending tagName but not all refernces used between starting
tagName and ending tagName. Is there any way to achive this?

It's not clear exactly what you mean. Could you give more information,
perhaps with an example XML file and preferrably a short but complete
piece of code too?
 
G

Guest

Please see the code and input file first :

In the following code, output i get is "<header href="#ref-3"/>"
but i didn't get what #ref-3 means. Actually I also wanted the content
"<Header id="ref-3" >
<title id="34">Exam title</title>
<description id="67">Exam description</description>
</Header>"
as it is part of "Exam" Node.

I wanted to use this information in different method.

File soap.txt:
<Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Body>
<Exam id="ref-1" >
<header href="#ref-3"/>
</Exam>
<Header id="ref-3" >
<title id="34">Exam title</title>
<description id="67">Exam description</description>
</Header>
</Body>
</Envelope>


Code :
public static void Main()
{
XmlDocument doc = new XmlDocument();
doc.ToString();
doc.Load("C:\\soap.txt");

//Display all the book titles.
XmlNodeList elemList = doc.GetElementsByTagName("Exam");
for (int i=0; i < elemList.Count; i++)
{
Console.WriteLine(elemList.InnerXml);
}
 
J

Jon Skeet [C# MVP]

Omkar Singh said:
Please see the code and input file first :

In the following code, output i get is "<header href="#ref-3"/>"
but i didn't get what #ref-3 means.

Right. You've retrieved exactly what you've asked for though - the
inner piece of XML for the node.
Actually I also wanted the content
"<Header id="ref-3" >
<title id="34">Exam title</title>
<description id="67">Exam description</description>
</Header>"
as it is part of "Exam" Node.

I wanted to use this information in different method.

You'll have to use a separate way of doing that - eg by finding all
elements with an "href" attribute whose value starts with a "#", and
substituting the contents of those elements with the ones retrieved
using GetElementById with the appropriate ID.
 
G

Guest

Thanks for replying but the problem with solution you mention is I am doing
all these operation on string which is in XML format. For using
GetElementById we need Attribute define in DTD.
Is there an way to use GetElementById on string.
 
J

Jon Skeet [C# MVP]

Omkar Singh said:
Thanks for replying but the problem with solution you mention is I am doing
all these operation on string which is in XML format. For using
GetElementById we need Attribute define in DTD.
Is there an way to use GetElementById on string.

Ah, I missed that. In that case, just use an XPath expression (and
SelectSingleNode) to find a node with the right attribute.
 
G

Guest

Thanks Jon. It is working!
Jon I hav one more problem. My string contains some namespaces(soap and
cwnp) other than default xml namespace. When I use LoadXml() method it gives
me error "'soap' is an undecleared namespace". Is there any way to overcome
this problem.
 
J

Jon Skeet [C# MVP]

Omkar Singh said:
Jon I hav one more problem. My string contains some namespaces(soap and
cwnp) other than default xml namespace. When I use LoadXml() method it gives
me error "'soap' is an undecleared namespace". Is there any way to overcome
this problem.

Yes - make sure your XML declares all the namespaces it uses.
 
G

Guest

Sorry Jon I didn't get it. which XML you mention. I am using string as xml
file.
Do you mean to add namespace to these string? If yes how?
 
J

Jon Skeet [C# MVP]

Omkar Singh said:
Sorry Jon I didn't get it. which XML you mention. I am using string as xml
file.
Do you mean to add namespace to these string? If yes how?

Yes - it sounds like your string isn't *really* a valid XML document.
Try to make it one, by properly declaring in it the namespaces you're
using. To be honest, I'm not very hot on XML namespaces, but I'm sure
there are good tutorials around which should help you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top