Solved

XML Document - reorder attributes

Posted on 2010-11-30
7
681 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

705 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