Creating DTD using DOM

  • Thread starter Thread starter Michel Verhagen
  • Start date Start date
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
 
Hi Michel,

The problem is, DTD, unlike XSD, is not XML itself - hence you cannot create
it using the DOM API.
Yet, why would you stick to a DTD at all? Do you need compatibility with
some legacy code? Unless something is "holding you back", I'd recommend that
you use XSD instead.
 
Thank you for your reply Dmytro,

As I said, I'm a total newbie to XML stuff. I will look into XSD and see
how that works. Maybe you can give me some pointers or example on how to
do what I want?

Thanks!

Michel.
Hi Michel,

The problem is, DTD, unlike XSD, is not XML itself - hence you cannot
create it using the DOM API.
Yet, why would you stick to a DTD at all? Do you need compatibility with
some legacy code? Unless something is "holding you back", I'd recommend
that you use XSD instead.

Michel Verhagen said:
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
 
Michel,

The direct answer to your question is here:

http://www.vijaymukhi.com/documents/books/xsd/chap7.htm

However, I'd also recommend that you read the following materials:

- An introduction to XSD schemas:
http://www.vijaymukhi.com/documents/books/xsd/chap2.htm
- System.Xml.Schema namespace docs in MSDN library.
- Formal XSD definition that can be found on w3.org:
http://www.w3.org/XML/Schema#dev

Michel Verhagen (eMVP) said:
Thank you for your reply Dmytro,

As I said, I'm a total newbie to XML stuff. I will look into XSD and see
how that works. Maybe you can give me some pointers or example on how to
do what I want?

Thanks!

Michel.
Hi Michel,

The problem is, DTD, unlike XSD, is not XML itself - hence you cannot
create it using the DOM API.
Yet, why would you stick to a DTD at all? Do you need compatibility with
some legacy code? Unless something is "holding you back", I'd recommend
that you use XSD instead.

Michel Verhagen said:
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
 
Thank you very much, you've been very helpful!

Regards,

Michel.
Michel,

The direct answer to your question is here:

http://www.vijaymukhi.com/documents/books/xsd/chap7.htm

However, I'd also recommend that you read the following materials:

- An introduction to XSD schemas:
http://www.vijaymukhi.com/documents/books/xsd/chap2.htm
- System.Xml.Schema namespace docs in MSDN library.
- Formal XSD definition that can be found on w3.org:
http://www.w3.org/XML/Schema#dev

Michel Verhagen (eMVP) said:
Thank you for your reply Dmytro,

As I said, I'm a total newbie to XML stuff. I will look into XSD and
see how that works. Maybe you can give me some pointers or example on
how to do what I want?

Thanks!

Michel.
Hi Michel,

The problem is, DTD, unlike XSD, is not XML itself - hence you cannot
create it using the DOM API.
Yet, why would you stick to a DTD at all? Do you need compatibility
with some legacy code? Unless something is "holding you back", I'd
recommend that you use XSD instead.

message 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
 
Back
Top