Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XmlSerialization: How to add attribute to an element

Status
Not open for further replies.

johngrg

Programmer
Apr 19, 2007
17
US
I am using XmlSerializer library to serialize object into xml. Here's what I get currently.

<?xml version="1.0" encoding="utf-8"?>
<Root xmlns:xsi=" xmlns:xsd=" <Expenses>
<Expense>
---
---
</Expense>
</Expenses>
</Root>

How can I add an attribute count='1' inside the <Expenses> element? so it looks like <Expenses count ='1'>




public static void SerializeDocument(string filename, List<Expense> expenses)
{
Root root = new Root();
root.expenses = expenses;

XmlSerializer serializer = new XmlSerializer(typeof(Root));
TextWriter writer = new StreamWriter(filename);
serializer.Serialize(writer, root);
writer.Close();
}


[XmlRootAttribute("Root", IsNullable = true)]
public class Root
{
[XmlArray(ElementName = "Expenses")]
public List<Expense> expenses;

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top