0

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?

Sss
  • 1,519
  • 8
  • 37
  • 67
  • "Before i used `XmlElement` for this" - do you mean `XmlSerializer`? (`XmlElement` wouldn't do that; `XmlSerializer` would) – Marc Gravell Jun 24 '14 at 11:40
  • @MarcGravell No i have already done it using XmlSerializer.It shows all sep together and all pr togetehr. I want to have them sequentially but i couldnt find have to do that using XElement. – Sss Jun 24 '14 at 11:45
  • I tried it with second part of code but cursor do not enter foreach (var param in pc.Pr) {here } in this loop because Pr has nothing inside. So how to achieve it using XElement ? – Sss Jun 24 '14 at 11:46
  • exactly... so you have in fact **not** "used `XmlElement` for this"; you have used `XmlSerializer` for it, and (separately) *failed* to use `XmlElement` for it... yes? – Marc Gravell Jun 24 '14 at 11:48
  • When using `XElement`, you are building the xml yourself - but with formatting support; for example: http://pastie.org/9319395 – Marc Gravell Jun 24 '14 at 11:48
  • Yes, I actually do not understand properly. OK i explain you in detail: Before i had xml and i created Objects of the parent class in xml and i accesed all the xml and stored in class objects. Now i try to do opposite. I have class objects and i have to create xml from that and before i was using "XmlElement" but it do not maintain the sequence of "pr" and "sep". So thats why i asked yoy for help. – Sss Jun 24 '14 at 11:52
  • @MarcGravell but you have not used any field of my classes like "Type" ,"name", "Comp" and "attr" etc. in the code you have written on the link.(As i was doing before like this " Pr = { new Pr { Name = "Name1", Component = { new Comp { Type = "Type1", Attr = { new Attr { Type = "Com" } } } } }). I ALSO HAVE TO USE THESE FIELDS(i apologize if i cal it by wrong name "field"). Actually i am trying to implement serialization. – Sss Jun 24 '14 at 12:16
  • @MarcGravell by not using "XmlSerializer" do you mean not using at this lin : XmlSerializer deserializer = new XmlSerializer(typeof(Ps)); ? – Sss Jun 24 '14 at 12:56
  • @MarcGravell I HAVE EXPLAINE DAGAIN THE QUESTION WTH AN EDIT ; SO THAT YOU WILL UNDERSTAND WHAT I WANT TO DO. – Sss Jun 24 '14 at 13:06
  • I posted this on your other, worse, question and i think it is still relevant if you put forth the effort to understand it. http://stackoverflow.com/questions/19049964/c-sharp-serialize-nested-class – Austinh100 Jun 24 '14 at 13:24
  • You repeat the same link everywhere. Please read the question carefully.WHAT MY PROBLEM IS and WHAT MY QUESTION SAYS. the link you have sent will not work for me(ihave already done that and it the xml obtained using the procdeure on your link will not maintain the sequence of "ps" and "sep" one after the other ot will be like all "ps" together and all "sep" together. pLEASE READ THE QUESTION AGAIN) – Sss Jun 24 '14 at 13:28
  • 1
    @user234839 what you *want* to do is largely irrelevant, when faced with reality (unless you are volunteering to write a serialization library that bridges the gap between the two things); if you want to use the object model, you are basically doing serialization - and we already know that this doesn't achieve the output that you want. So: forget that. Instead, you are going to have to build the xml model directly. I've had somewhere between 3 and 5 conversations on the same topic, but ultimately you're going to have to do the legwork here. I can't volunteer to write it for you. – Marc Gravell Jun 24 '14 at 14:54
  • @MarcGravell sorry i couldn't understand (you write a bit complicated language). So you mean there is no alternative to achieve this target ? – Sss Jun 24 '14 at 14:59
  • @user234839 what I am saying is that since `XmlSerializer` (which works against an object model) won't work the way you want, you need to do something different. `XElement` is one such approach. `XmlDocument` is another. As is `XmlWriter` (but more complex). But: working directly from the object-model via `XmlSerializer`: *isn't*. – Marc Gravell Jun 24 '14 at 15:30
  • @MarcGravell I tried xmlWriter but it's too much manual to do it because we do things like this: "writer.WriteElementString("Name", prs[0].Parameter[0].Name);" . Do you think that it is possible by "XmlChoiceIdentifierAttribute " ? (I am not experienced thats why asking you once. I am still stuck here badly since last 2 weeks). – Sss Jun 26 '14 at 09:07
  • 1
    @user234839 no, I don't think that is an appropriate solution; I've already given you my advice: construct and parse a DOM. I've shown you broadly how. I'm not sure what it is you want here, but again: I'm *not* going to simply write the entire thing for you. – Marc Gravell Jun 26 '14 at 10:07
  • Ok..I found the soultion for it :) – Sss Jun 26 '14 at 11:59

0 Answers0