我需要生成這個簡單的XML,尋找一個乾淨的方式來生成它。你將如何在c#中構建這個xml
<order>
<user>2343></user>
<creditcardtype>2333></creditcarttype>
<country>USA</country>
<orderDetails>
<amount>23434</amount>
<shipping>32</shipping>
</orderDetails>
</order>
我需要生成這個簡單的XML,尋找一個乾淨的方式來生成它。你將如何在c#中構建這個xml
<order>
<user>2343></user>
<creditcardtype>2333></creditcarttype>
<country>USA</country>
<orderDetails>
<amount>23434</amount>
<shipping>32</shipping>
</orderDetails>
</order>
由於XDocument
被採取,這是一個XmlWriter
答案:
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();
或者與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();
我很確定XmlWriter方法是最快的,至少它是基於我所做的基準測試。如果您想要序列化對象模型而不是直接生成XML,那麼WCF DataContractSerializer也很快。 – 2009-11-24 14:39:39
+10給馬克..不僅爲了答案,而且還努力解釋兩種方法。 – Dienekes 2010-10-27 11:13:37
看到LINQ to XMl方式做到這一點,像這樣
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);
等
使用的XDocument類,所以代碼會像
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")
)
)
);
xsd.exe
根據給定的XML文件自動生成模式。)xsd.exe /classes
根據您的XML模式生成C#類。XmlSerializer
類將您的XML序列化/反序列化爲C#對象結構。還有一個codeproject article描述這種方法。
這也是所有提到的最慢的方法。 – 2009-11-24 14:37:02
我猜XmlDocument方法更慢...無論如何,我喜歡它的一切都是強類型的,你甚至可以做一個模式驗證。 – Vinz 2009-11-24 14:46:34
我用的LINQ到XML的建議,但出於完整性我只需要添加這一個:
var xe = XElement.Parse("<order><user>2343</user><creditcardtype>2333</creditcarttype><country>USA</country><orderDetails><amount>23434</amount><shipping>32</shipping></orderDetails></order>");
:)
任何理由,你爲什麼不會崩潰之類的用戶ID變成屬性?即:' ...' –
2009-11-24 12:47:50
如果他正在調用非SOAP Web服務,他可能需要一個這樣格式化的XML。 – 2009-11-24 12:57:36
格式不在我手中不幸。 – mrblah 2009-11-24 13:53:00