mrblah
mrblah

Reputation: 103487

How would you build this xml in c#

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>

Upvotes: 5

Views: 304

Answers (6)

Jakob Gade
Jakob Gade

Reputation: 12419

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>");

:)

Upvotes: 0

Vinz
Vinz

Reputation: 2005

  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.

Upvotes: 1

Marc Gravell
Marc Gravell

Reputation: 1062510

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();

Upvotes: 11

Arsen Mkrtchyan
Arsen Mkrtchyan

Reputation: 50712

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");

Upvotes: 4

Card Visitor
Card Visitor

Reputation: 145

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")

        )
    )
);

Upvotes: 11

Nick Allen
Nick Allen

Reputation: 12200

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

Upvotes: 2

Related Questions