How to use Xelement in this situation

2019-03-05 22:08发布

I have to obtain this kind of 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>

You can see that i have ps as parent of all and after that i have pr and sep in a sequence and i want to maintain this sequence. Before i used XmlElement for this but it was showing all sep togetehr and all pr together. I guess i need to use XElement for it but i don't know how to use them. Could some one please explain me or write a piece of code to make refrence to understand hos i will obtain this kind of xml.

My try to do it is : (which do not give what i want)

[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 = "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 { Separators = "sep1" ..and so on} }  

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

But this code dont give sequence as i want.It shows in xml first all the sep togther and then ps togther(not in sequence) I have achieved it using XElement but it is static and do not use my class objects like "Name", "Comp", Type, Value etc. It's like this:

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..

So it not at all use the Class Objects. We assigne her elike this XElement("name", "name1") but i want something like this ps Object1 = new ps(); and Object1.pr[0].Name= "name1";

How to achieve this and it should maintain the same sequence of ps and sep as well?

0条回答
登录 后发表回答