[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

XML Document - reorder attributes

Posted on 2010-11-30
7
Medium Priority
?
690 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:Meir Rivkin
ID: 34239749
post the whole xml and i'll write the code which reorder the attributes
0
 
LVL 42

Expert Comment

by:Meir Rivkin
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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:Meir Rivkin
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:
Meir Rivkin earned 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

656 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