Home All Groups Group Topic Archive Search About

Creating DTD using DOM

Author
22 May 2006 10:51 PM
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

Author
23 May 2006 8:06 AM
Dmytro Lapshyn [MVP]
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.

Show quote
"Michel Verhagen" <mverhagen@nospam.embeddedfusion.com> wrote in message
news:OCLKGJffGHA.3916@TK2MSFTNGP04.phx.gbl...
> 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
Author
23 May 2006 8:25 AM
Michel Verhagen (eMVP)
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.

Dmytro Lapshyn [MVP] wrote:
Show quote
> 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" <mverhagen@nospam.embeddedfusion.com> wrote in message
> news:OCLKGJffGHA.3916@TK2MSFTNGP04.phx.gbl...
>> 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
>
Author
24 May 2006 7:25 AM
Dmytro Lapshyn [MVP]
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

Show quote
"Michel Verhagen (eMVP)" <michel.verhagen@nospam.embeddedfusion.com> wrote
in message news:OPF33JkfGHA.3916@TK2MSFTNGP04.phx.gbl...
> 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.
>
> Dmytro Lapshyn [MVP] wrote:
>> 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" <mverhagen@nospam.embeddedfusion.com> wrote in message
>> news:OCLKGJffGHA.3916@TK2MSFTNGP04.phx.gbl...
>>> 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
>>
Author
24 May 2006 9:49 PM
Michel Verhagen (eMVP)
Thank you very much, you've been very helpful!

Regards,

Michel.

Dmytro Lapshyn [MVP] wrote:
Show quote
> 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)" <michel.verhagen@nospam.embeddedfusion.com>
> wrote in message news:OPF33JkfGHA.3916@TK2MSFTNGP04.phx.gbl...
>> 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.
>>
>> Dmytro Lapshyn [MVP] wrote:
>>> 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" <mverhagen@nospam.embeddedfusion.com> wrote in
>>> message news:OCLKGJffGHA.3916@TK2MSFTNGP04.phx.gbl...
>>>> 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 Verhagen, eMVP
Embedded Fusion
www.embeddedfusion.com
mverhagen at embeddedfusion dot com

AddThis Social Bookmark Button