468,544 Members | 1,711 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,544 developers. It's quick & easy.

Creating DTD using DOM

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
May 22 '06 #1
4 1683
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" <mv*******@nospam.embeddedfusion.com> wrote in message
news:OC**************@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


May 23 '06 #2
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" <mv*******@nospam.embeddedfusion.com> wrote in message
news:OC**************@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

May 23 '06 #3
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)" <mi*************@nospam.embeddedfusion.com> wrote
in message news:OP**************@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" <mv*******@nospam.embeddedfusion.com> wrote in message
news:OC**************@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


May 24 '06 #4
Thank you very much, you've been very helpful!

Regards,

Michel.

Dmytro Lapshyn [MVP] wrote:
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)" <mi*************@nospam.embeddedfusion.com>
wrote in message news:OP**************@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" <mv*******@nospam.embeddedfusion.com> wrote in
message news:OC**************@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
May 24 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Altramagnus | last post: by
15 posts views Thread by Carlos Lozano | last post: by
15 posts views Thread by David Thielen | last post: by
17 posts views Thread by Lee Harr | last post: by
11 posts views Thread by Matthew Wells | last post: by
9 posts views Thread by =?Utf-8?B?YmJn?= | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.