TRICKY关于列表<t>到XML </t>的问题

时间:2011-03-26 15:05:17

标签: c# xml serialization

关于对象列表的浅层序列化我有一个棘手的问题

我的实体

public class Requests
{
    public string Action { get; set; }
    public Meta MetaType { get; set; }

}

public class Meta
{
    public string MerchantId { get; set; }
    public string IpAddress { get; set; }
    public string Version { get; set; }
}

使用XMLSerializer进行浅层序列化

    List<Requests> lstXML = new List<Requests>();

    Requests xml = new Requests();
    xml.Action = "INSERT";
    xml.MetaType = new Meta { IpAddress = "192.2.3.4", MerchantId = "DALE", Version = "1" };
    lstXML.Add(xml);
    xml = new Requests();
    xml.Action = "UPDATE";
    xml.MetaType = new Meta { IpAddress = "192.2.3.40", MerchantId = "SECD", Version = "1" };
    lstXML.Add(xml);


    using (var sw = new StreamWriter(@"C:\XML\test.txt"))
    {
        var serializer = new XmlSerializer(typeof(List<Requests>));
        serializer.Serialize(sw, lstXML);

    }

输出textfile text.txt

  <?xml version="1.0" encoding="utf-8"?>
 <ArrayOfRequests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Requests>
  <Action>INSERT</Action>
   <MetaType>
  <MerchantId>DALE</MerchantId>
  <IpAddress>192.2.3.4</IpAddress>
  <Version>1</Version>
</MetaType>
   </Requests>
 <Requests>
<Action>UPDATE</Action>
<MetaType>
  <MerchantId>SECD</MerchantId>
  <IpAddress>192.2.3.40</IpAddress>
  <Version>1</Version>
</MetaType>
     </Requests>
    </ArrayOfRequests>
  

现在我的问题是

     

1)我需要删除&lt; ?XML   version =“1.0”....&gt;和   &LT; ArrayOfRequests ...&gt;    标记并仅保留   我的实体的XML标签。我怎么能够   那样做?

     

2)我如何资本化(CAPS)   元素名称( - &gt;   )在输出文本文件?

我想要的文本文件输出是

<XML>
 <REQUESTS>
 <ACTION>INSERT</ACTION>
 <META>
   <MERCHANTID>DALE</MERCHANTID>
   <IPADDRESS>202.164.178.163</IPADDRESS>
   <VERSION>1</VERSION>
 </META>
 <REQUESTS>

 <REQUESTS>
 <ACTION>INSERT</ACTION>
 <META>
   <MERCHANTID>DALE</MERCHANTID>
   <IPADDRESS>202.164.178.163</IPADDRESS>
   <VERSION>1</VERSION>
 </META>
 <REQUESTS>

 </XML>

先谢谢你们! =)

3 个答案:

答案 0 :(得分:3)

尝试

[XmlType("REQUESTS")]  
public class Requests
{
    [XmlElement(ElementName="ACTION")]
    public string Action { get; set; }

    [XmlElement(ElementName="META")]
    public Meta MetaType { get; set; }
    ...

}

public class Meta
{

    [XmlElement(ElementName="MERCHANTID")]
    public string MerchantId { get; set; }
    ...
}

var serializer = new XmlSerializer(typeof(List<Requests>), new XmlRootAttribute("XML"));

答案 1 :(得分:1)

因此,您不希望XML序列化。你想要一个自定义近似。因此,您可能需要自己编写。你使用任何一种模板语言吗?如果您知道要预先序列化的类型,模板系统将对您有所帮助。如果您事先不知道类型,请下一个答案。

答案 2 :(得分:1)

绝对最简单的解决方案可能会通过几个后处理步骤来处理当前生成的字符串:

// Stream to a string instead of directly to the file
yourString = // stream XML here.

// Remove the xmlns stuff.
yourString = yourString.Replace(
          " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"", 
          ""); 

// Upper-case the start-elements and end-elements (your data has no attributes, 
// so keep it simple)
yourString = Regex.Replace(yourString, 
                           "<\{0,1}([^>]+)>", 
                           delegate(Match m) 
                           {
                               return m.Value.ToUpper(); 
                           });
相关问题