Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading id Attribute in XML (XSD) file

Status
Not open for further replies.

RickBeddoe

Programmer
Nov 18, 2008
32
US
I was going to post this in the XML forum, but I think this is more C# related.

I have the following schema file:

1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE xs:schema [
3 <!ELEMENT xs:schema ANY>
4 <!ELEMENT xs:element ANY>
5 <!ATTLIST xs:element id ID #REQUIRED>
6 ]>
7
8 <xs:schema xmlns:xs="9 <xs:element id="MyElement" type="Monetary" />
10 </xs:schema>



I have the following code to access this document and element:

1 XmlDocument dtsAll = new XmlDocument();
2 string dtsAllName =@"c:\mySchema.xsd";
3 dtsAll.Load(dtsAllName);
4 XmlElement dtsAllElm = dtsAll.GetElementById(attr);



This will allow me to then access the 'type' attribute and extract it's value:

1 string allType = dtsAllElm.GetAttribute("type");



I would like to know if there is a way to do this without using DTD (either embedded as shown or linked). I have looked at using the XSLT and Schema namespace, but I'm not exactly sure how to implement them.

Thanks,

Rick
 
Where do you think you have used the DTD? I can tell that you've not been using it. If the approach is up to the need, jst keep it.
 
In order for C# to use the GetElementByID method, the attribute you wish to use as an ID attribute needs to be defined as such in the DTD. This is per MS documentation that GetElementById does not currently work with Schema files and requires DTD. Note line 5 in my DTD. That is where the attribute 'ID' is defined as the ID attribute to be used by the GetElementByID.

I have since resolved this problem by prepending the DTD to an instance of the XSD document then using LoadXML. If you'd like, I can post the code for this.

This is a lot of monkeying around just to be able to use the GetElementByID, but it has increased the performance of my application considerably. Prior to this I needed to iterate through all the elements defined in my schema (12,000 elements) to find a match.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top