Unable to use GetElementById

G

Guest

I am unable to select a specific XmlElement from my xml document. I have an
xml file with the following data:

<?xml version="1.0" encoding="utf-8"?>
<my_data>
<dogs>
<dog id="benji" breed="mutt">
<dog id="Lassie" breed="collie">
</dogs>
</my_data>

If I create an XmlDataSource using the xml file the following line of code
will return null. How can I get this to work?

XmlElement myDog = MyXmlDataSource.GetXmlDocument().GetElementById("benji");
 
G

Guest

I am unable to select a specific XmlElement from my xml document. I have an
xml file with the following data:

<?xml version="1.0" encoding="utf-8"?>
<my_data>
<dogs>
<dog id="benji" breed="mutt">
<dog id="Lassie" breed="collie">
</dogs>
</my_data>

If I create an XmlDataSource using the xml file the following line of code
will return null. How can I get this to work?

XmlElement myDog = MyXmlDataSource.GetXmlDocument().GetElementById("benji");

GetElementById requires a DTD or an XML schema to indicate which
attribute has to be used as an id. In your case you need to define the
following

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE my_data [
<!ELEMENT my_data ANY>
<!ELEMENT dogs ANY>
<!ELEMENT dog EMPTY>
<!ATTLIST dog id ID #REQUIRED>
<!ATTLIST dog breed CDATA #REQUIRED>
]>
<my_data>
<dogs>
<dog id="benji" breed="mutt" />
<dog id="Lassie" breed="collie" />
</dogs>
</my_data>

Then it would work with your code. For more information about
GetElementById Method please refer MSDN:
http://msdn2.microsoft.com/en-us/library/system.xml.xmldocument.getelementbyid.aspx

You can also do another method, e.g. SelectSingleNode, in this case it
would work without changes in XML

XmlNode myDog = MySource.GetXmlDocument().SelectSingleNode("//
*[@id='benji']");

Hope it helps
 
G

Guest

Thanks Alexey,
I tried the SelectSingleNode function and that worked great


Anon User said:
I am unable to select a specific XmlElement from my xml document. I have an
xml file with the following data:

<?xml version="1.0" encoding="utf-8"?>
<my_data>
<dogs>
<dog id="benji" breed="mutt">
<dog id="Lassie" breed="collie">
</dogs>
</my_data>

If I create an XmlDataSource using the xml file the following line of code
will return null. How can I get this to work?

XmlElement myDog = MyXmlDataSource.GetXmlDocument().GetElementById("benji");

GetElementById requires a DTD or an XML schema to indicate which
attribute has to be used as an id. In your case you need to define the
following

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE my_data [
<!ELEMENT my_data ANY>
<!ELEMENT dogs ANY>
<!ELEMENT dog EMPTY>
<!ATTLIST dog id ID #REQUIRED>
<!ATTLIST dog breed CDATA #REQUIRED>
]>
<my_data>
<dogs>
<dog id="benji" breed="mutt" />
<dog id="Lassie" breed="collie" />
</dogs>
</my_data>

Then it would work with your code. For more information about
GetElementById Method please refer MSDN:
http://msdn2.microsoft.com/en-us/library/system.xml.xmldocument.getelementbyid.aspx

You can also do another method, e.g. SelectSingleNode, in this case it
would work without changes in XML

XmlNode myDog = MySource.GetXmlDocument().SelectSingleNode("//
*[@id='benji']");

Hope it helps
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top