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>
<use开发者_JS百科r>2343></user>
<creditcardtype>2333></creditcarttype>
<country>USA</country>
<orderDetails>
<amount>23434</amount>
<shipping>32</shipping>
</orderDetails>
</order>
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();
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")
)
)
);
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");
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
- Write an XML schema describing your structure. (You could also use
xsd.exe
to automatically generate the schema according to a given XML file.) - Use
xsd.exe /classes
to genereate C# classes according to your XML schema. - 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.
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>");
:)
精彩评论