tags:

views:

212

answers:

6

I need to generate this simple looking XML, looking for a clean way to generate it.

<order>
<user>2343></user>
<creditcardtype>2333></creditcarttype>
<country>USA</country>
<orderDetails>
   <amount>23434</amount>
   <shipping>32</shipping>
</orderDetails>
</order>
+4  A: 

see LINQ to XMl way to do that, something like this

XDocument doc = new XDocument(new XElement("order",
                   new XElement("user", "2343"),
                   new XElement("creditcardtype", "2333"),
                   new XElement("country", "USA"),
               new XElement("orderDetails",
                   new XElement("amount", "23434"),
                   new XElement("shipping", "32"))));
doc.Save("myxml.xml");
ArsenMkrt
+2  A: 
XmlDocument xml = new XmlDocument();

XmlElement order = xml.CreateElement("order");

xml.AppendChild(order);

XmlElement user = xml.CreateElement("user");
user.InnerText = "2343";
order.AppendChild(user);

XmlElement ccType = xml.CreateElement("creditcardtype");
ccType.InnerText = "2333";
order.AppendChild(ccType);

etc

Nick Allen - Tungle139
+10  A: 

use XDocument class, so code will be like

XDocument srcTree = new XDocument(
    new XElement("order",
        new XElement("user", "2343"),
        new XElement("creditcardtype", "2333"),
        new XElement("country", "USA"),
        new XElement("orderDetails",
            new XElement ("amount", "23434"),
            new XElement ("shipping", "32")

        )
    )
);
Saul
+10  A: 

Since XDocument is taken, here's an XmlWriter answer:

    StringWriter sw = new StringWriter();
    using (XmlWriter xw = XmlWriter.Create(sw)) {
        xw.WriteStartElement("order");
        xw.WriteElementString("user", "2343");
        xw.WriteElementString("creditcardtype", "2333");
        xw.WriteElementString("country", "USA");
        xw.WriteStartElement("orderDetails");
        xw.WriteElementString("amount", "23434");
        xw.WriteElementString("shipping", "32");
        xw.WriteEndElement();
        xw.WriteEndElement();
    }
    string s = sw.ToString();


Or with XmlSerializer:

[XmlRoot("order")] public class Order {
    [XmlElement("user")] public int User { get; set; }
    [XmlElement("creditcardtype")] public int CreditCardType { get; set; }
    [XmlElement("country")] public string Country { get; set; }
    [XmlElement("orderDetails")] public OrderDetails Details { get; set; }
}
public class OrderDetails {
    [XmlElement("amount")] public int Amount { get; set; }
    [XmlElement("shipping")] public int Shipping { get; set; }
}
....
var order = new Order {
    User = 2343, CreditCardType = 2333, Country = "USA",
    Details = new OrderDetails {
        Amount = 23434,
        Shipping = 32
    }
};
XmlSerializer ser = new XmlSerializer(order.GetType());
StringWriter sw = new StringWriter();
ser.Serialize(sw, order);
string s = sw.ToString();
Marc Gravell
I'm pretty sure the XmlWriter method is the fastest as well, at least it is based on benchmarks that I've done. The WCF DataContractSerializer is quick too, if you want to serialize an object model instead of generating the XML directly.
Chris Patterson
+10 to Marc.. Not only for the answer but also taking the effort to explain both the approaches as is.
Dienekes
+1  A: 
  1. Write an XML schema describing your structure. (You could also use xsd.exe to automatically generate the schema according to a given XML file.)
  2. Use xsd.exe /classes to genereate C# classes according to your XML schema.
  3. Now you can use the XmlSerializer class to serialize/deserialize your XML from/to a C# object-structure.

There is also a codeproject article describing this approach.

Vinz
This is also about the slowest method of all the ones mentioned.
Chris Patterson
I guess the XmlDocument approach is even slower... Anyway, what i like about it is that everything is strongly typed and you could even do a schema validation.
Vinz
A: 

I'd go with the Linq to XML suggestions, but for completeness I just have to add this one:

var xe = XElement.Parse("<order><user>2343</user><creditcardtype>2333</creditcarttype><country>USA</country><orderDetails><amount>23434</amount><shipping>32</shipping></orderDetails></order>");

:)

Jakob Gade