GetElementById

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.

[176 byte] By [OmkarSingh] at [2007-12-17]
# 1

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"?>
<
xs:schema elementFormDefault="qualified"
xmlns:xs
=" http://www.w3.org/2001/XMLSchema">
<
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/.

ChrisLovett at 2007-9-9 > top of Msdn Tech,.NET Development,XML and the .NET Framework...

.NET Development

Site Classified