Solved

XML Document - reorder attributes

Posted on 2010-11-30
7
671 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
  • 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now