0

我对 Visual Basic 和 XML 还有些陌生,所以我不知道我的措辞是否正确。我只触及了 XSLT 的表面,但就我目前所见,我不知道它是否可以做到这一点。

我试图在 Visual Basic 中按其标签的字母顺序对 XML 文件进行递归排序。例如,对于 XML 文档,例如:

<catalog>
  <game>
      <title>Role-playing EXTREME Adventure</title>
      <platform>PC</platform>
      <type>Action/Adventure</type>
      <price>60.00</price>
  </game>
  <cd>
      <title>Music Notes</title>
      <artist>Susan Dylan</artist>
      <genre>Rock</genre>
      <company>Columbia</company>
      <year>1995</year>
      <price>10.95</price>
  </cd>
  <book>
      <title>Pictures of Things</title>
      <author>Bob Smith</author>
      <type>paper-back</type>
      <price>5.99</price>
  </book>
</catalog>

...变成:

<catalog>
  <book>
      <author>Bob Smith</author>
      <price>5.99</price>
      <title>Pictures of Things</title>
      <type>paper-back</type>
  </book>
  <cd>
      <artist>Susan Dylan</artist>
      <company>Columbia</company>
      <genre>Rock</genre>
      <price>10.95</price>
      <title>Music Notes</title>
      <year>1995</year>
  </cd>
  <game>
      <platform>PC</platform>
      <price>60.00</price>
      <title>Role-playing EXTREME Adventure</title>
      <type>Action/Adventure</type>
  </game>
</catalog>

这个排序后的版本应该替换 XML 文件的内容并被保存。这将针对多个长 XML 文件完成。我不在我的工作计算机旁,所以我无法访问我一直在摆弄的混乱的 Visual Basic 代码。我开始认为我应该放弃我所做的一切。

如果在某处得到回答,我深表歉意。我花了大部分时间搜索和研究这个。因此,如果已经有答案,我期待获得它的链接。:)

我看到了这个,但它似乎不能满足我的需求。它必须在 Visual Basic 而不是命令行中完成。

我很感激任何帮助。

4

2 回答 2

1

这是一个可用于对 XML 进行排序的程序(您至少需要 .NET 3.0 才能使用 LINQ to XML 和扩展方法,但这也可以通过旧式 XML 解析库和常规方法来完成):

Imports System.Xml.Linq
Imports System.Runtime.CompilerServices

Module Module1

Sub Main()
    Dim xe As XElement = XElement.Load("test.xml")
    Console.WriteLine("----Original XML----")
    Console.WriteLine(xe)
    xe.SortRecursive()
    Console.WriteLine("----Sorted XML----")
    Console.WriteLine(xe)
End Sub

''' <summary>
''' Sorts the subelements of an XML element, and their subelements recursively.
''' </summary>
''' <param name="xe"></param>
''' <remarks></remarks>
<Extension()>
Sub SortRecursive(xe As XElement)
    xe.Sort()
    For Each subelement As XElement In xe.Elements()
        subelement.SortRecursive()
    Next
End Sub

''' <summary>
''' Sorts the subelements of an XML element.
''' </summary>
''' <param name="xe">The element whose subelements are to be sorted.</param>
<Extension()>
Sub Sort(xe As XElement)
    ' Save off the subelements into a list, and remove them from the main element
    Dim subelements As New List(Of XElement)
    For Each subelement As XElement In xe.Elements().ToArray()
        subelement.Remove()
        subelements.Add(subelement)
    Next

    ' Sort the list of subelements
    subelements.Sort(AddressOf CompareElementNames)

    ' Add the elements back and return when done
    For Each subelement As XElement In subelements
        xe.Add(subelement)
    Next
End Sub

''' <summary>
''' Compares two XML elements by their names.
''' </summary>
''' <param name="e1">The first element.</param>
''' <param name="e2">The second element.</param>
''' <returns>positive/negative/zero depending on comparison (same as string comparison)</returns>
Function CompareElementNames(e1 As XElement, e2 As XElement) As Integer
    Return String.Compare(e1.Name.ToString(), e2.Name.ToString())
End Function
End Module
于 2012-08-13T23:33:32.713 回答
0

这对我来说似乎很好用:

Dim recurse As Action(Of XElement) = Nothing
recurse = _
    Sub (x)
        For Each e in x _
                .Elements() _
                .OrderBy(Function (y) y.Name.LocalName) _
                .ToArray()
            recurse(e)
            e.Remove()
            x.Add(e)
        Next
    End Sub

我用这些数据进行了测试:

Dim xml = _
    <catalog>
        <game>
            <title>Role-playing EXTREME Adventure</title>
            <platform>PC</platform>
            <type>Action/Adventure</type>
            <price>60.00</price>
        </game>
        <cd>
            <title>Music Notes</title>
            <artist>Susan Dylan</artist>
            <genre>Rock</genre>
            <company>Columbia</company>
            <year>1995</year>
            <price>10.95</price>
        </cd>
        <book>
            <title>Pictures of Things</title>
            <author>Bob Smith</author>
            <type>paper-back</type>
            <price>5.99</price>
        </book>
    </catalog>

通过调用:

recurse(xml)

并得到了这个结果:

<catalog>
  <book>
    <author>Bob Smith</author>
    <price>5.99</price>
    <title>Pictures of Things</title>
    <type>paper-back</type>
  </book>
  <cd>
    <artist>Susan Dylan</artist>
    <company>Columbia</company>
    <genre>Rock</genre>
    <price>10.95</price>
    <title>Music Notes</title>
    <year>1995</year>
  </cd>
  <game>
    <platform>PC</platform>
    <price>60.00</price>
    <title>Role-playing EXTREME Adventure</title>
    <type>Action/Adventure</type>
  </game>
</catalog>
于 2012-08-14T02:10:04.167 回答