tags:

views:

414

answers:

3
+1  Q: 

xml, linq parsing

I am modifying my xml using LINQ

Dim feedXML As XDocument = XDocument.Parse(m_xmld.OuterXml.ToString())
Dim SortedFields = From field In feedXML.Descendants("fields")
Dim sFieldList = From field In SortedFields.Descendants("field") Order By 
   Integer.Parse(field.@position)

what I am trying to do is sort my "fields" in an ascending order. Now my problem is I want the sorted fields to replace my unsorted fields list in the xml so that I can use the sorted xml further.

Anybody knows how can I retrive the xml after the sorting?

Thanks

A: 

There's no real concept of "the xml after the sorting". If you've only got field elements, it's relatively easy - but if you've got:

 <field position="2" />
 <non-field />
 <field position="1" />
 <non-field />
 <field position="0" />

then what should the result be afterwards?

Jon Skeet
Nope, Basically what I want to know is after I am done with modifying the nxml(i.e sorting the fields in the node "fields") i want to modified xml to do some other operations, how can i get the modified xml?
Mithil Deshmukh
There *isn't* any modified XML. You haven't been sorting the XML itself - you've been sorting a list fetched from the XML. That's my point. You haven't been changing the XML itself at all. Given the example in my answer, what do you want the result to be?
Jon Skeet
Sorry if i framed the question in a wrong way,yes, I am sorting the list fetched from the xml. Now i want this list to replaced the unsorted list in my original xml so that I can use it.Thanks
Mithil Deshmukh
So what do you want the result to be in my example? Or will the XML only have the field elements?
Jon Skeet
I have other elements in the xml< report>< sql >< fields>< field position="1"/>< field position="3"/>< field position="2"/></fields></sql></report>Now i am soring the <fields> node , so now i want this sorted nodelist to replace the unsorted list in the original xml.
Mithil Deshmukh
I want the following result. <field position="0" /> <non-field /> <field position="1" /> <non-field /> <field position="2" />
Mithil Deshmukh
Okay. I'll see whether I can work out a way of doing that. It's likely to be a bit of a pain, to be honest...
Jon Skeet
Hey Jon, thanks for your help. I figured it out, my mentor had written something which i used to make it work.Thanks a lot again!
Mithil Deshmukh
+1  A: 

I just did this. I needed to pass it on to another function that was expecting a xml object so I did the following this is c# code though.

public static XElement xelmSort(this XElement xelm)
    {
        List<XElement> newType = new List<XElement>();
        List<XElement> typeOne = new List<XElement>(xelm.Elements("type One"));
        List<XElement> typeTwo = new List<XElement>(xelm.Elements("type Two"));
        typeTwo.Sort((c, d) => d.Attribute("name").Value.CompareTo(c.Attribute("name").Value));
        typeOne.Sort((c, d) => d.Attribute("name").Value.CompareTo(c.Attribute("name").Value));
        typeOne.ForEach(c => newType.Add(c.xelmSort()));


        XElement outElm = new XElement(xelm.Name, xelm.Attribute("name"), xelm.Element("info"));

        newType.ForEach(outElm.Add);
        typeTwo.ForEach(outElm.Add);
        return outElm;
    }

After I was finished I needed to convert it back to an old style xml node so i used the following. This function will return a XmlDocument so you need to get the first child on it to get the node you started out with. Hope this helps.

public static XmlNode GetXmlNode(this XElement element)
    {
        using (XmlReader xmlReader = element.CreateReader())
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlReader);
            return xmlDoc;
        }
    }
Erin
A: 

I used ReplaceNodes for this in the end:

x.ReplaceNodes(
    from el in x.Elements()
    orderby (int)el.Element("Index")
    select el                               
);
Gert Jan Kamstra