Solved

XML Document - reorder attributes

Posted on 2010-11-30
7
680 Views
Last Modified: 2012-05-10
Hi

I have some XML file, in the document the attributes are in wrong order.  How do I reorder the attributes of an element?

e.g. data...

 <ABC FieldB="...." FieldC="...." FieldA="..." FieldD="1"></ABC>

needs to be

 <ABC FieldA="...." FieldB="...." FieldC="..." FieldD="1"></ABC>

Thanks in advance
0
Comment
Question by:rwallacej
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 34239749
post the whole xml and i'll write the code which reorder the attributes
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 34239805
C# or vb.net?
0
 

Author Comment

by:rwallacej
ID: 34239872
hi, thanks but unfortunately I can't post the XML for it contains IP.

I found a "crude" way using search/replace on InnerText - but I thought there would be a simpler way
0
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 

Author Comment

by:rwallacej
ID: 34239891
it would be interesting to see the code, this is XML sample (in wrong order)

<MyXML>
 <ABC FieldB="...." FieldC="...." FieldA="..." FieldD="1"></ABC>
 <ABC FieldB="...." FieldC="...." FieldA="..." FieldD="1"></ABC>
 <ABC FieldB="...." FieldC="...." FieldA="..." FieldD="1"></ABC>
</MyXML>

correct...

 <ABC FieldA="...." FieldB="...." FieldC="..." FieldD="1"></ABC>
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 34239892
can u post the xpath to the elements where you want to reorder the attribute?
for example if the xml is this:

<xml>
<config>
<settings>
 <ABC FieldB="...." FieldC="...." FieldA="..." FieldD="1"></ABC>
<<settings>
</config>
</xml>

the xpath is this:

xml/config/settings/ABC
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 34240050
here's a sample code which based on your xml
XElement root = XElement.Load(@"c:\temp\1.xml");
            foreach (XElement item in root.XPathSelectElements("ABC"))
            {
                var attributes = item.Attributes().OrderBy(n => n.Name.LocalName);
                List<XAttribute> tmp = new List<XAttribute>();
                foreach (var attr in attributes)
                {
                    tmp.Add(new XAttribute(attr.Name, attr.Value));
                } 
                item.RemoveAttributes();
                foreach (XAttribute xa in tmp)
                {
                    item.Add(xa);
                }
            }
            root.Save(@"c:\temp\2.xml");

Open in new window

0
 

Author Closing Comment

by:rwallacej
ID: 34359338
great help thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question