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 4 1771
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
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,
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
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 This discussion thread is closed Replies have been disabled for this discussion. Similar topics
reply
views
Thread by Robert |
last post: by
|
4 posts
views
Thread by Altramagnus |
last post: by
|
8 posts
views
Thread by Nanda |
last post: by
|
15 posts
views
Thread by Carlos Lozano |
last post: by
|
3 posts
views
Thread by Ken Varn |
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
| | | | | | | | | | |