M
Michel Verhagen
Hi,
I want to create a DTD in an XML file in memory. The XML file is created
using the DOM in C#. I am new to all this, but couldn't find anything
about creating DTD's using the DOM (well, I found some references but it
doesn't tell me how to do what I want).
Here's my code:
XmlDocument xdoc = new XmlDocument();
XmlElement root = xdoc.DocumentElement;
XmlDeclaration xdec = xdoc.CreateXmlDeclaration("1.0", "utf-8", null);
xdoc.InsertBefore(xdec, root);
xdoc.AppendChild(xdoc.CreateDocumentType("Config", null, null,
"<!ELEMENT Config (Shows, Schedules)*>"));
The last line will add one DTD entry to my XML file, but I want several
more. Unfortunately, appending more DTD entries using AppendChild
doesn't work since the XML document can have only one document type node.
So this will result in an error:
xdoc.AppendChild(xdoc.CreateDocumentType("Shows", null, null, "<!ELEMENT
Shows (Show)*>"));
I want to create this DTD:
<!DOCTYPE Config [
<!ELEMENT Config (Shows, Schedules)*>
<!ELEMENT Shows (Show)*>
<!ELEMENT Show (title, url, livestream, savefolder, filename?, keepold)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT url (#PCDATA)>
<!ELEMENT livestream (#PCDATA)>
<!ELEMENT savefolder (#PCDATA)>
<!ELEMENT filename (#PCDATA)>
<!ELEMENT keepold (#PCDATA)>
<!ELEMENT Schedules (Schedule)*>
<!ELEMENT Schedule (whenchanged, ((start, stop) | interval | days |
monthday))>
<!ELEMENT whenchanged (#PCDATA)>
<!ELEMENT start (#PCDATA)>
<!ELEMENT stop (#PCDATA)>
<!ELEMENT interval (#PCDATA)>
<!ELEMENT days (#PCDATA)>
<!ELEMENT monthday (#PCDATA)>
<!ATTLIST Show id ID #REQUIRED>
<!ATTLIST Schedule id ID #REQUIRED>
<!ATTLIST Schedule showid REFID #REQUIRED>
So besides creating the elements, I also want to add attributes, but I
have no idea how to do that.
Another question: the ID field in the attribute list tells the XML
parser that the id attribute needs to be unique. Is there any way to get
a unique number automatically from the DOM? Or should I scan through the
id's and keep a list of used ids?
To summarize:
1. How to create multiple <!ELEMENT entries using DOM
2. How to create multiple <!ATTLIST entries where I can specify ID
#REQUIRED using DOM
3. How to get a unique ID from the DOM
Thank you for your time!
Michel Verhagen
I want to create a DTD in an XML file in memory. The XML file is created
using the DOM in C#. I am new to all this, but couldn't find anything
about creating DTD's using the DOM (well, I found some references but it
doesn't tell me how to do what I want).
Here's my code:
XmlDocument xdoc = new XmlDocument();
XmlElement root = xdoc.DocumentElement;
XmlDeclaration xdec = xdoc.CreateXmlDeclaration("1.0", "utf-8", null);
xdoc.InsertBefore(xdec, root);
xdoc.AppendChild(xdoc.CreateDocumentType("Config", null, null,
"<!ELEMENT Config (Shows, Schedules)*>"));
The last line will add one DTD entry to my XML file, but I want several
more. Unfortunately, appending more DTD entries using AppendChild
doesn't work since the XML document can have only one document type node.
So this will result in an error:
xdoc.AppendChild(xdoc.CreateDocumentType("Shows", null, null, "<!ELEMENT
Shows (Show)*>"));
I want to create this DTD:
<!DOCTYPE Config [
<!ELEMENT Config (Shows, Schedules)*>
<!ELEMENT Shows (Show)*>
<!ELEMENT Show (title, url, livestream, savefolder, filename?, keepold)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT url (#PCDATA)>
<!ELEMENT livestream (#PCDATA)>
<!ELEMENT savefolder (#PCDATA)>
<!ELEMENT filename (#PCDATA)>
<!ELEMENT keepold (#PCDATA)>
<!ELEMENT Schedules (Schedule)*>
<!ELEMENT Schedule (whenchanged, ((start, stop) | interval | days |
monthday))>
<!ELEMENT whenchanged (#PCDATA)>
<!ELEMENT start (#PCDATA)>
<!ELEMENT stop (#PCDATA)>
<!ELEMENT interval (#PCDATA)>
<!ELEMENT days (#PCDATA)>
<!ELEMENT monthday (#PCDATA)>
<!ATTLIST Show id ID #REQUIRED>
<!ATTLIST Schedule id ID #REQUIRED>
<!ATTLIST Schedule showid REFID #REQUIRED>
So besides creating the elements, I also want to add attributes, but I
have no idea how to do that.
Another question: the ID field in the attribute list tells the XML
parser that the id attribute needs to be unique. Is there any way to get
a unique number automatically from the DOM? Or should I scan through the
id's and keep a list of used ids?
To summarize:
1. How to create multiple <!ELEMENT entries using DOM
2. How to create multiple <!ATTLIST entries where I can specify ID
#REQUIRED using DOM
3. How to get a unique ID from the DOM
Thank you for your time!
Michel Verhagen