如何对这个嵌套的xml进行序列化

时间:2014-06-24 13:48:51

标签: c# .net xml serialization xml-serialization

我正处于尝试进行序列化的情况:

我的课程是这样的:

 [XmlRoot(ElementName = "ps")]
  public class Ps
  {
   [XmlElement("sep")]
   public List<Sep> Sep { get { return a1; } }
   private readonly List<Sep> a1 = new List<Sep>(); 

   [XmlElement("pr")]
   public List<Pr> Pr { get { return b1; } }
   private readonly List<Pr> b1 = new List<Pr>();
 }


 [XmlRoot(ElementName = "attr")]
public class Attr
{     
    [XmlElement("type")],
    public string Type { get; set; }

    [XmlElement("value")]
    public int Value { get; set; }

}

    [XmlRoot(ElementName = "pr")]
          public class Pr
        {
            [XmlElement("name")]
            public string Name { get; set; }

            [XmlElement("comp")]
            public List<Comp> Comp { get { return b3; } }
            private readonly List<Comp> b3 = new List<Comp>();

        }
 [XmlRoot(ElementName = "sep")]
    public class Sep
    {
        [XmlElement("sep")]
        public string Sep { get; set; }

    }

 public void Main()
{

            Ps pc = new Ps()
              {
                Pr = { new Pr { Name = "Name1",  Component = { new Comp { Type = "CB", Attr = { new Attr { Value = "0" } } } } }, { new Pr { Name = "Name2" ,Comp = { new Comp { Type = "RB", Attr = { new Attr { Value = "l" ....And so On} } } } } } }
             ,
                Sep = { new Sep { Sep = "sep1" ..and so on} }  

              };
            var xml = pc.ToXml(); 
}

获得的xml应该是这样的:

<ps>
    <pr>
        <name>name1</name>
        <comp>
            <type>CB</type>
            <attr>
                <value>0</value>
            </attr>
        </comp>
    </pr>
    <sep>sep1</sep>
    <pr>
        <name>name2</name>
        <comp>
            <type>RB</type>
            <attr>
                <value>1</value>
            </attr>
        </comp>
    </pr>
    <sep>sep2</sep>
    <pr>
        <name>name3</name>
        <comp>
            <type>CoM</type>
            <attr>
                <value>2</value>
            </attr>
        </comp>
    </pr>
    <sep>sep3</sep>
</ps>

但不幸的是它是这样的:

<ps>
  <sep>sep1</sep>
  <sep>sep2</sep>
  <sep>sep3</sep>
  <pr>
      <name>name1</name>
       <comp>
            <type>CB</type>
            <attr>
                <value>0</value>
            </attr>
       </comp>
    </pr>    
    <pr>
        <name>name2</name>
        <comp>
            <type>RB</type>
            <attr>
                <value>1</value>
            </attr>
        </comp>
    </pr>    
    <pr>
        <name>name3</name>
        <comp>
            <type>CoM</type>
            <attr>
                <value>2</value>
            </attr>
        </comp>
    </pr>  
</ps>

xml包含所有&#34; sep&#34;在一起和所有&#34; pr&#34;一起。如何保持&#34; pr&#34;和&#34; sep&#34;因为它应该被获得?   我尝试使用XElemnt,但问题是我们在那里静态地做每一个,就像这样:

var el =   new XElement("ps",
                       new XElement("pr",
                           new XElement("name", "name1"),
                           new XElement("comp",
                               new XElement("type", "CB"),
                               new XElement("attr",
                                   new XElement("value", 0)
                               )
                           )
                       ),//And so on..

我也试过这样的事情:

 Ps pc = new Ps()
              {
                Pr = { new Pr { Name = "Name1",  Comp = { new Comp { Type = "Type1", Attr = { new Attr { Type = "Com" } } } } }, { new Pr { Name = "Name2" ,Comp = { new Comp { Type = "Type2", Attr = { new Attr { Type = "Sl" ....And so On} } } } } } }
             ,
                Sep = { new Sep { Sep = "sep1" ..and so on} }  
              , Pr = { new Pr { Name = "Name1",  Component //so on 
              ,Sep = { new Sep { Sep = "sep1" ..and so on} }  
              };
        var xml = pc.ToXml(); 

这将不会影响Pr ans sep的初始化。

我们像XElement("name", "name1")这样分配,但我想要这样的ps Object1 = new ps(); and Object1.pr[0].Name= "name1"; 怎么做到这一点?

编辑:我尝试了代码&#34;在Austinh的评论之后:

public void Main()
{
   List<object> lst = new List<object>();
   Ps pc = new Ps()
   {
          Pr = { new Pr { Name = "Name1",Comp = { new Comp { Type = "Type1", Attr = { new Attr { Type = "Combo"} } } } }}
     ,
          Sep = { new Sep{ Sep = "AutoSkew1" } }
    };

   Ps pc1 = new Ps()
   {
       Pr = { new Pr { Name = "Name3", Comp = { new Comp { Type = "Type3", Attr = { new Attr { Type = "Rb"} } } } } }
   ,
       Sep = { new Sep { Sep = "AutoSkew2" }}
   };
   lst.Add(pc);
   lst.Add(pc1);
   var xml = lst.ToXml();
}

但是我有无效的用户处理:生成XML文档时出错。在下面的代码中的serializer.Serialize(writer, objectToSerialize);行中:

public static string ToXml(this object objectToSerialize)
        {
            var writer = new StringWriter();
            var serializer = new XmlSerializer((objectToSerialize.GetType()));
            serializer.Serialize(writer, objectToSerialize);
            string xml = writer.ToString();
            return xml;
        }

1 个答案:

答案 0 :(得分:1)

我没有对此进行测试,因此它可能无法正常工作,但这里有一种可能的方法来构建这样的结构。

改编自:c# serialize nested class

public class Ps
{
    [XmlArray("ps")]
    public List<object> items { get { return a1; } }
    private readonly List<object> a1 = new List<object>(); 
}

然后在需要时将(sep或pr)添加到Ps列表中,这将保持添加到的顺序。

扩展主编辑(暗示执行上述代码):

public void Main()
{
   Ps pc = new Ps();
   pc.items.Add(new Pr { Name = "Name1",Comp = { new Comp { Type = "Type1", Attr = { new Attr { Type = "Combo"} } } } });
   pc.items.Add(new Sep{ Sep = "AutoSkew1" });

   pc.items.Add(new Pr { Name = "Name3", Comp = { new Comp { Type = "Type3", Attr = { new Attr { Type = "Rb"} } } } });
   pc.items.Add(new Sep { Sep = "AutoSkew2" });
   var xml = pc.items.ToXml();
}