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

本文关键字:xml 序列化 嵌套 | 更新日期: 2023-09-27 18:10:01

我在一个情况下,我试图做序列化:

我的类是这样的:

 [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包含所有"sep"和所有"pr"。如何保持"pr"answers"sep"的顺序,因为它应该得到?我尝试使用xelement,但问题是我们静态地做所有事情,像这样:

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/sep。

我们在这里这样赋值XElement("name", "name1"),但我想要这样赋值ps Object1 = new ps(); and Object1.pr[0].Name= "name1";如何做到这一点?

编辑:在austin的评论之后,我尝试了下面的代码:

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;
        }

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

我还没有测试过,所以它可能不起作用,但这里有一种可能的方法来拥有这样的结构。

改编自:c# serialize嵌套类

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