Hi I wrote such code it works mostly perfect(you may use it)
but I don"t know how to add attributes to some TAGs for example I want to change Expire date to attribute of buyer
I want you suggesting how to add attributes
class Program
{
static void Main(string[] args)
{
Book[] books = FillBooks();
FillXmlDoc(books);
}
//Fill Function
static Book[] FillBooks()
{
Book[] books=new Book[5];
for(int i=0;i<books.Length;i++)
{
books[i]=new Book("Terminator"+i,"Sergey M"+i,5.50+i*2,"King",DateTime.Today.AddDays(i+2));
}
return books;
}
//
private static void FillXmlDoc(Book[] books)
{
//Name of XML file
string filename="Books.xml";
//Encoding and spacing
XmlTextWriter write = new XmlTextWriter(filename, Encoding.UTF8);
write.Formatting=Formatting.Indented;
write.Indentation=3;
write.WriteStartDocument();
write.WriteStartElement("Books");
foreach (Book b in books)
{
write.WriteStartElement("Book");
write.WriteElementString("Name",b.Name);
write.WriteElementString("Author",b.Author);
write.WriteElementString("Price", b.Price.ToString());
write.WriteStartElement("Expire");
write.WriteElementString("Day", b.Expire.Day.ToString());
write.WriteElementString("Month", b.Expire.Month.ToString());
write.WriteElementString("Year", b.Expire.Year.ToString());
}
write.WriteEndElement();
write.WriteEndDocument();
write.Close();
}
}
}
and here the help class for this program
public class Book
{
private string name;
private string author;
private double price;
private List<String> buyers= new List<String>();
private DateTime expire;
public string Name
{
get{ return name;}
set{ name=value; }
}
public string Author
{ get{return author;}
set{author=value;}
}
public double Price
{
get{ return price;}
set{price=value;}
}
public List<string> Buyers
{
get { return buyers; }
set { buyers = value; }
}
public DateTime Expire
{
get { return expire; }
set { expire = value; }
}
//ctor
public Book()
{
}
public Book(string name,string author,double price,string buyers,DateTime expire)
{
Name = name;
Author = author;
Price = price;
Buyers.Add(buyers);
Expire = expire;
}
}
}