Solved

XML Document - reorder attributes

Posted on 2010-11-30
7
670 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

16 Experts available now in Live!

Get 1:1 Help Now