你将如何构建这个XML在C# [英] How would you build this xml in c#

查看:112
本文介绍了你将如何构建这个XML在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要生成这个简单的XML寻找,寻找一个干净的方式来生成它。

 <排序>
<使用者> 2343>< /使用者名称>
<&CREDITCARDTYPE GT; 2333>< / creditcarttype>
<国家>美国< /国家>
<&ORDERDETAILS GT;
   <量GT; 23434< /量GT;
   <运费和GT; 32 LT; /送货>
< / ORDERDETAILS>
< /排序>


解决方案

由于的XDocument 拍摄,这里是一个的XmlWriter 答:

 的StringWriter SW =新的StringWriter();
    使用(XmlWriter的XW = XmlWriter.Create(SW)){
        xw.WriteStartElement(命令);
        xw.WriteElementString(用户,2343);
        xw.WriteElementString(CREDITCARDTYPE,2333);
        xw.WriteElementString(国家,USA);
        xw.WriteStartElement(ORDERDETAILS);
        xw.WriteElementString(额,23434);
        xw.WriteElementString(送货,32);
        xw.WriteEndElement();
        xw.WriteEndElement();
    }
    字符串s = sw.ToString();


,使用的XmlSerializer

  [XmlRoot(订单)]公共类订单{
    [的XmlElement(用户)]公众诠释用户{搞定;组; }
    [的XmlElement(CREDITCARDTYPE)]公众诠释CREDITCARDTYPE {搞定;组; }
    [的XmlElement(国家)]公共字符串国家{搞定;组; }
    [的XmlElement(订单明细)]公共订单明细详细{搞定;组; }
}
公共类订单明细{
    [XmlElement的(额)]公众诠释金额{搞定;组; }
    [的XmlElement(运输)]公众诠释航运{搞定;组; }
}
....
VAR为了=新订单{
    用户= 2343,CREDITCARDTYPE = 2333,国家=USA,
    详细=新订单明细{
        金额= 23434,
        航运= 32
    }
};
XmlSerializer的SER =新的XmlSerializer(order.GetType());
StringWriter的SW =新的StringWriter();
ser.Serialize(SW,顺序);
字符串s = sw.ToString();

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>

解决方案

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

这篇关于你将如何构建这个XML在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆