Unable to use GetElementById

Discussion in 'ASP .Net' started by =?Utf-8?B?Rmx5Z3V5?=, Oct 7, 2007.

  1. 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");
     
    =?Utf-8?B?Rmx5Z3V5?=, Oct 7, 2007
    #1
    1. Advertising

  2. On Oct 7, 9:57 pm, Flyguy <> 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
     
    Alexey Smirnov, Oct 7, 2007
    #2
    1. Advertising

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


    "Alexey Smirnov" wrote:

    > On Oct 7, 9:57 pm, Flyguy <> 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
    >
    >
     
    =?Utf-8?B?Rmx5Z3V5?=, Oct 7, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. AAaron123

    How to use getElementById

    AAaron123, Jun 19, 2009, in forum: ASP .Net
    Replies:
    5
    Views:
    1,345
    AAaron123
    Jun 19, 2009
  2. Jake Barnes
    Replies:
    6
    Views:
    451
    9.co.uk
    Feb 8, 2006
  3. Nicholas

    How to use getelementbyid with functions

    Nicholas, Apr 25, 2007, in forum: Javascript
    Replies:
    3
    Views:
    96
    Nicholas
    Apr 26, 2007
  4. JCK
    Replies:
    7
    Views:
    192
  5. Tim Streater

    Wanting to use getElementById()

    Tim Streater, Dec 9, 2011, in forum: Javascript
    Replies:
    17
    Views:
    865
    Gene Wirchenko
    Dec 15, 2011
Loading...

Share This Page