1

I have generated the xml document using XDocument and create a Document type declaration (DTD).

But i could not validate the DTD against the generated XML.

What is the best way to check whether the DTD is properly created or not?

Is there any way in c# to dynamically generate the DTD?

I have explicitly created a InternalSubset in my code, what is the purpose of adding the name bookstoredoc.dtd in the line below in XDocumentType decleration?

XDocumentType docType = new XDocumentType("bookstoredoc", null,
 "bookstoredoc.dtd",
 @"<!ELEMENT bookstore (book+)>
 <!ELEMENT book (title|year|price|author+) >
 <!ATTLIST book
 category CDATA #REQUIRED>
 <!ELEMENT title (#PCDATA)>
 <!ATTLIST title
 lang CDATA #REQUIRED>
 <!ELEMENT year (#PCDATA)>
 <!ELEMENT price (#PCDATA)>
 <!ELEMENT author (name+)>
 <!ELEMENT name (#PCDATA)>");

Code:

var bookObj = new BookStore();
var bookList = bookObj.GetBookStores();
XDocumentType docType = new XDocumentType("bookstoredoc", null,
 "bookstoredoc.dtd",
 @"<!ELEMENT bookstore (book+)>
 <!ELEMENT book (title|year|price|author+) >
 <!ATTLIST book
 category CDATA #REQUIRED>
 <!ELEMENT title (#PCDATA)>
 <!ATTLIST title
 lang CDATA #REQUIRED>
 <!ELEMENT year (#PCDATA)>
 <!ELEMENT price (#PCDATA)>
 <!ELEMENT author (name+)>
 <!ELEMENT name (#PCDATA)>");
var xmlDocument = new XDocument(docType, new XElement("bookstore",
 bookList.Select(x =>
 new XElement("book", new XAttribute("Name", x.Category),
 new XElement("title", new XAttribute("lang", x.Lang), x.Title),
 new XElement("year", x.Year),
 new XElement("price", x.Price),
 new XElement("author", x.Author.Select(y => new XElement("name", y.Name)))
 ))));
xmlDocument.Save(@"D:\Test.xml");

BookStore.cs

public class BookStore
{
 public BookCategory Category { get; set; }
 public string Title { get; set; }
 public List<Author> Author { get; set; }
 public int Year { get; set; }
 public double Price { get; set; }
 public string Lang { get; set; }
 public List<BookStore> GetBookStores()
 {
 var list = new List<BookStore>()
 {
 new BookStore
 {
 Category = BookCategory.COOKING,
 Title = "Everyday Italian",
 Author = new List<Author>()
 {
 new Author{ Name = "Giada De Laurentiis"}
 },
 Year = 2005,
 Price = 30.00,
 Lang = "en"
 }, new BookStore
 {
 Category = BookCategory.CHILDREN,
 Title = "Harry Potter",
 Author = new List<Author>()
 {
 new Author{ Name = "J K. Rowling"}
 },
 Year = 2005,
 Price = 29.99,
 Lang = "en"
 },new BookStore
 {
 Category = BookCategory.WEB,
 Title = "XQuery Kick Start",
 Author = new List<Author>()
 {
 new Author{ Name = "James McGovern"},
 new Author{ Name = "Per Bothner"},
 new Author{ Name = "Kurt Cagle"},
 new Author{ Name = "James Linn"},
 new Author{ Name = "Vaidyanathan Nagarajan"},
 },
 Year = 2003,
 Price = 49.99,
 Lang = "en"
 },new BookStore
 {
 Category = BookCategory.WEB,
 Title = "Learning XML",
 Author = new List<Author>()
 {
 new Author{ Name = "Erik T. Ray"}
 },
 Year = 2003,
 Price = 39.95,
 Lang = "en"
 }
 };
 return list;
 }
}
public enum BookCategory
{
 COOKING,
 CHILDREN,
 WEB
}
public class Author
{
 public string Name { get; set; }
}
asked Aug 7, 2019 at 17:14

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.