rwheeler23
asked on
VS C# Writing XML
I have just begun looking into how to write an xml file using visual studio c#. I came across this sample code and I can see I need to define 'book' someplace. How do I correct this code to define the proper structure for book? It has the Title, Author.Name, Publisher, Price attributes.
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = (" ");
settings.CloseOutput = true;
settings.OmitXmlDeclaratio n = true;
using (XmlWriter writer = XmlWriter.Create("books.xm l", settings))
{
writer.WriteStartElement(" book");
writer.WriteElementString( "title", book.Title);
writer.WriteElementString( "author", book.Author.Name);
writer.WriteElementString( "publisher ", book.Publisher);
writer.WriteElementString( "price", book.Price.ToString());
writer.WriteEndElement();
writer.Flush();
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = (" ");
settings.CloseOutput = true;
settings.OmitXmlDeclaratio
using (XmlWriter writer = XmlWriter.Create("books.xm
{
writer.WriteStartElement("
writer.WriteElementString(
writer.WriteElementString(
writer.WriteElementString(
writer.WriteElementString(
writer.WriteEndElement();
writer.Flush();
It would help if you showed the XML structure you are trying to achieve.
ASKER
My apologies. Here it is:
<?xml version="1.0" encoding="utf-8" ?>
<!-- Books.xml stores information about Mahesh Chand and related books -->
<books>
<book ISBN="9831123212" yearpublished="2002">
<title>A Programmer's Guide to ADO.NET using C#</title>
<author>
<first-name>Mahesh</first- name>
<last-name>Chand</last-nam e>
</author>
<publisher>Apress</publish er>
<price>44.99</price>
</book>
</books>
<?xml version="1.0" encoding="utf-8" ?>
<!-- Books.xml stores information about Mahesh Chand and related books -->
<books>
<book ISBN="9831123212" yearpublished="2002">
<title>A Programmer's Guide to ADO.NET using C#</title>
<author>
<first-name>Mahesh</first-
<last-name>Chand</last-nam
</author>
<publisher>Apress</publish
<price>44.99</price>
</book>
</books>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Excellent. Thank you.
Kaufmed's answer is the correct answer. However, I wonder if your actual question is more basic. You need a class or structure defined that has the object you want, right?
First, you need to create your classes:
Next, the code to write this is almost exactly like kaufmed's solution:
Here's the actual output from this code:
I came across this sample code and I can see I need to define 'book' someplace. How do I correct this code to define the proper structure for book? It has the Title, Author.Name, Publisher, Price attributes.
First, you need to create your classes:
public class Author
{
public struct AuthorName
{
[XmlElement("first-name")]
public string First_Name;
[XmlElement("last-name")]
public string Last_Name;
};
private AuthorName name = new AuthorName();
public AuthorName Name
{
get
{
return name;
}
set
{
name = value;
}
}
public Author(string first, string last)
{
name = new AuthorName();
name.First_Name = first;
name.Last_Name = last;
}
}
public class Book
{
public string Title { get; set; }
public string Publisher { get; set; }
public decimal Price { get; set; }
public Author Author { get; set; }
public string ISBN { get; set; }
public string Year_Published { get; set; }
}
Next, the code to write this is almost exactly like kaufmed's solution:
public void doIt()
{
Book book = new Book();
book.Title = "A Programmer's Guide to ADO.NET using C#";
book.Price = 44.99M;
book.Publisher = "Apress";
book.Author = new Author("Mahesh", "Chanad");
book.ISBN = "9831123212";
book.Year_Published = "2002";
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = (" ");
settings.CloseOutput = true;
settings.OmitXmlDeclaration = true;
using (XmlWriter writer = XmlWriter.Create(@"C:\books.xml", settings))
{
writer.WriteStartElement("books");
writer.WriteStartElement("book");
writer.WriteAttributeString("ISBN", book.ISBN);
writer.WriteAttributeString("Year-Published", book.Year_Published);
writer.WriteElementString("title", book.Title);
writer.WriteStartElement("author");
writer.WriteElementString("first-name", book.Author.Name.First_Name);
writer.WriteElementString("last-name", book.Author.Name.Last_Name);
writer.WriteEndElement();
writer.WriteElementString("publisher", book.Publisher);
writer.WriteElementString("price", book.Price.ToString());
writer.WriteEndElement();
writer.WriteEndElement();
writer.Flush();
}
}
Here's the actual output from this code:
<books>
<book ISBN="9831123212" Year-Published="2002">
<title>A Programmer's Guide to ADO.NET using C#</title>
<author>
<first-name>Mahesh</first-name>
<last-name>Chanad</last-name>
</author>
<publisher>Apress</publisher>
<price>44.99</price>
</book>
</books>
ASKER
That though dawned on me as I working on some other code. I continue to be impressed by the knowledge that is shared on this website. Thanks to all of you my code gets cleaner and more efficient with each application.