How to use XmlDocument.GetElementById method
How to use XmlDocument.GetElementById method
How to use XmlDocument.GetElementById method
How to use XmlDocument.GetElementById method
How to use XmlDocument.GetElementById method
How to use XmlDocument.GetElementById method How to use XmlDocument.GetElementById method How to use XmlDocument.GetElementById method How to use XmlDocument.GetElementById method How to use XmlDocument.GetElementById method How to use XmlDocument.GetElementById method How to use XmlDocument.GetElementById method How to use XmlDocument.GetElementById method
How to use XmlDocument.GetElementById method How to use XmlDocument.GetElementById method
How to use XmlDocument.GetElementById method
Go Back  Xtreme Visual Basic Talk > > > How to use XmlDocument.GetElementById method


Reply
 
Thread Tools Display Modes
  #1  
Old 01-08-2010, 05:11 AM
4x2y 4x2y is offline
Contributor
 
Join Date: Sep 2005
Posts: 566
Default How to use XmlDocument.GetElementById method


How can i use XmlDocument.GetElementById method? It is always return Nothing

Code:
    Private mXmlDoc As XmlDocument

    Public Sub Read(ByVal strFileName As String)
        mXmlDoc = New XmlDocument
        mXmlDoc.Load(strFileName)
    End Sub

   Public Function GetXmlElementByID(ByVal strID As String) As XmlElement
       Return mXmlDoc.GetElementById(strID)
   End Function
here is a snap of the xml file
HTML Code:
<?xml version="1.0" encoding="utf-8"?>
<root>
 <group>
      <item id="1">
        <name>name 1</name>
        <description>description 1</description>
      </item>
      <item id="2">
        <name>name 2</name>
        <description>description 2</description>
      </item>
      <item id="3">
        <name>name 3</name>
        <description>description 3</description>
      </item>
  </group>
</root>
I tried id capital (ID) and small (id), also tried id as attribute and as a child node of <item>
__________________
Visit my site

Last edited by 4x2y; 01-08-2010 at 05:25 AM.
Reply With Quote
  #2  
Old 01-08-2010, 10:00 AM
AtmaWeapon's Avatar
AtmaWeaponHow to use XmlDocument.GetElementById method AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

The documentation explains:
Quote:
Note:

The DOM implementation must have information which defines which attributes are of type ID. Although attributes of type ID can be defined in either XSD schemas or DTDs, this version of the product only supports those defined in DTDs. Attributes with the name "ID" are not of type ID unless so defined in the DTD. Implementations where it is unknown whether the attributes are of type ID are expected to return Nothing.
If you don't have a DTD that specifies which attribute is the ID attribute, then apparently this method doesn't work. You can use XPath to get around it I guess:
Code:
Dim element As XmlNode = doc.SelectSingleNode("//*[@id=1]")
This line of code says: "Find any element anywhere in the file that has an "id" attribute that is set to the string value 1. If it does not exist, return Nothing."
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #3  
Old 01-09-2010, 01:41 AM
4x2y 4x2y is offline
Contributor
 
Join Date: Sep 2005
Posts: 566
Default

Quote:
Originally Posted by AtmaWeapon View Post
You can use XPath to get around it I guess:
Code:
Dim element As XmlNode = doc.SelectSingleNode("//*[@id=1]")

Thanks a lot, XPath works fine, I have changed my function as follow:

Code:
Public Function GetXmlElementByID(ByVal strID As String) As XmlElement Return mXmlDoc.SelectSingleNode("//*[@id='" & strID & "']") End Function

Later I will see how to define DTD for my XML file.
__________________
Visit my site
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
How to use XmlDocument.GetElementById method
How to use XmlDocument.GetElementById method
How to use XmlDocument.GetElementById method How to use XmlDocument.GetElementById method
How to use XmlDocument.GetElementById method
How to use XmlDocument.GetElementById method
How to use XmlDocument.GetElementById method How to use XmlDocument.GetElementById method How to use XmlDocument.GetElementById method How to use XmlDocument.GetElementById method How to use XmlDocument.GetElementById method How to use XmlDocument.GetElementById method How to use XmlDocument.GetElementById method
How to use XmlDocument.GetElementById method
How to use XmlDocument.GetElementById method
 
How to use XmlDocument.GetElementById method
How to use XmlDocument.GetElementById method
 
-->