Is there any way to use GetElementById on a string which is in XML form.
Currently when I try to use GetElementById it return null as the string is not in DTD form.
Is there any way to use GetElementById on a string which is in XML form.
Currently when I try to use GetElementById it return null as the string is not in DTD form.
GetElementById
RookieDBA
This sample works using VS 2005:
XmlDocument doc = new XmlDocument();
doc.LoadXml(@"<!DOCTYPE foo [
<!ELEMENT foo (item)*>
<!ELEMENT item EMPTY>
<!ATTLIST item id ID #REQUIRED>]>
<foo>
<item id='test'/>
<item id='xyz'/
<item id='abc'/>
</foo>");
XmlElement e = doc.GetElementById("xyz");
Console.WriteLine(e.OuterXml);
The trick is that you have to have some attribute of type ID defined using a DTD. If you have the following instead:
< xml version="1.0" encoding="utf-16" >http://www.w3.org/2001/XMLSchema ">
<xs:schema elementFormDefault="qualified"
xmlns:xs="
<xs:element name="foo">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element ref="item" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item">
<xs:complexType>
<xs:attribute name="id" type="xs:ID" use="required" />
</xs:complexType>
</xs:element>
</xs:schema>
then it will not work and this is a known limitation. You can file feature request on http://lab.msdn.microsoft.com/productfeedback/.