English 中文(简体)
午餐会
原标题:Sorting Attributes of Xml
  • 时间:2011-10-22 16:04:17
  •  标签:
  • c#
  • xml

我的投入就是如此:

<Phrase Entry="ID">
 <Ans number="1">
  <Identification LastName="Bornery" Name="John" Age="23"/>
 </Ans>
</Phrase>

and I want to sort the Xml attributes base on their first character Names order by alphabetic arrangment such as the blew Xml:

<Phrase Entry="ID">
 <Ans number="1">
  <Identification Age="23" LastName="Bornery" Name="John" />
 </Ans>
</Phrase>

感谢。

最佳回答

你的产出xml是错误的,但如果投入一样的话:

<Phrase Entry="ID">
 <Ans number="1">
  <Blah LastName="Bornery" Name="John" Age="23"/>
 </Ans>
</Phrase>

随后,以下法典

static string SortAttributes(string xml)
{
    var doc = XDocument.Parse(xml);
    foreach (XElement element in doc.Descendants())
    {
        var attrs = element.Attributes().ToList();
        attrs.Remove();
        attrs.Sort((a, b) => a.Name.LocalName.CompareTo(b.Name.LocalName));
        element.Add(attrs);
    }
    xml = doc.ToString();
    return xml;
}

归还

<Phrase Entry="ID">
  <Ans number="1">
    <Blah Age="23" LastName="Bornery" Name="John" />
  </Ans>
</Phrase>
问题回答

如果你想将两份XML文件作为指示加以比较,那么你就应当将其转换成Conical XML。 这不仅需要将属性变成一种 can令:例如,它涉及白天空间的正常化,也许指名胜地。 寻找一种XML的教化用途。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签