1

Here is my XML:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE taxes[
<!ELEMENT tax (name,percent,description)>
<!ATTLIST tax id CDATA #REQUIRED>
<!ELEMENT name (#PCDATA)>
<!ELEMENT percent (#PCDATA)>
<!ELEMENT description (#PCDATA)>
]>
<taxes>
    <tax id="Tax-1">
        <name>Tax 1</name>
        <percent>6.75</percent>
        <description>Ohio sales tax</description>
    </tax>
</taxes>

And my C# code:

XmlDocument doc = new XmlDocument();
doc.Load("path-to-file");

XmlElement element = doc.GetElementById("Tax-1");

Whenever I try to do stuff with element, I get a NullReferenceException.

I did some research and I found that a valid DTD is required, but as far as I know everything is valid...

Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
djmordigal
  • 549
  • 1
  • 5
  • 16

2 Answers2

3

Your DTD is incorrect. For your XML proper DTD is:

<!DOCTYPE taxes[
<!ELEMENT taxes (tax*)>
<!ELEMENT tax (name,percent,description)>
<!ATTLIST tax id CDATA #REQUIRED>
<!ELEMENT name (#PCDATA)>
<!ELEMENT percent (#PCDATA)>
<!ELEMENT description (#PCDATA)>
]>
Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
  • 1
    Fixed it! Thank you so much. I also changed the CDATA for "id" to ID and that seems to work fine as well. – djmordigal Jan 14 '15 at 03:45
0
<!ATTLIST tax id ID #REQUIRED>

Your id attribute on your tax element could also be called something other than id, e.g., taxid:

<!ATTLIST tax taxid ID #REQUIRED>
John Castleman
  • 1,552
  • 11
  • 12