I want to generate an XML of the following format using XmlWriter class in C# -:
<?xml version="1.0" ?>
<root>
<data>
<entry Attrib1="" Attrib2="91.3467" Attrib3="95.3052" Attrib4="6.4722" />
<entry Attrib1="" Attrib2="91.3467" Attrib3="95.3052" Attrib4="6.4722" />
</data>
</root>
I am very new to XmlWriter class and to C# in general and I have tried writing code for generating the file with the above format, but that attempt was unsuccessful
var xmlWriter = XmlWriter.Create(filename);
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("data");
xmlWriter.WriteStartElement("entry");
xmlWriter.WriteAttributeString("attrib1", "value1");
xmlWriter.WriteAttributeString("attrib2", "value2");
xmlWriter.Close();
also, the name of the attributes can included illegal XML characters and that's why I read up on XMLWriter because it seems to remove those illegal characters from the names of the attributes for instance a name like "this is attribute 1" should be reduced to something like "this_is_attribute_1" when written to the resulting XML, how do I go about producing such XML using XmlWriter. In short a row of the resulting XML is something like this
<entry P_B_Pe="" P_E_Pe="91.3467" Custom_Price="95.3052" C_Yield="6.4722" Average_Life="" />
You've almost got it...
var xmlWriter = XmlWriter.Create(filename);
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("root");
xmlWriter.WriteStartElement("data");
xmlWriter.WriteStartElement("entry");
xmlWriter.WriteAttributeString("attrib1", "value1");
xmlWriter.WriteAttributeString("attrib2", "value2");
xmlWriter.WriteEndElement(); // entry
xmlWriter.WriteStartElement("entry");
xmlWriter.WriteAttributeString("attrib1", "value1");
xmlWriter.WriteAttributeString("attrib2", "value2");
xmlWriter.WriteEndElement(); // entry
xmlWriter.WriteEndElement(); // data
xmlWriter.WriteEndElement(); // root
xmlWriter.WriteEndDocument();
xmlWriter.Close();
By default XmlWriter
will encode characters that are not normally valid in raw data or in attribute values in such a way that they will come back when you use a reader decode the XML, but attribute and element names must still be valid. If you want to handle invalid characters for those in some special way that's different from that, you'll need to do that yourself according to whatever rules you want to establish, something like:
xmlWriter.WriteAttributeString(MyXmlExtensions.EncodeXmlAttributeName("this is normally an invalid attribute name"), "value1");
class MyXmlExtensions
{
public string EncodeXmlAttributeName(string decoded)
{
// not that you'll likely need to enhance this with whatever rules you want but haven't specified
return decoded.Replace(" ", "_");
}
public string DecodeXmlAttributeName(string encoded)
{
// not that you'll likely need to enhance this with whatever rules you want but haven't specified
return encoded.Replace("_", " ");
}
}
You'll also need to use XmlWriterSettings
in the call to XmlWriter.Create
if you want the output to look pretty (tabs, multiple lines, etc.).