Welcome Guest Search | Active Topics | Members | Log In | Register

Re: Unable to use GetElementById Options · View
Alexey Smirnov
Posted: Sunday, October 07, 2007 7:51:17 AM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 07/10/2007 07:51:17
Date: Sun, 07 Oct 2007 14:51:17 -0700

On Oct 7, 9:57 pm, Flyguy <fly...@nospam.nospam> wrote:
> 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

Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFPro Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.1 (NET v2.0) - 9/10/2007
Copyright © 2003-2006 Yet Another Forum.net. All rights reserved.
This page was generated in 0.031 seconds.