按逻辑事件排序XML元素 [英] Sorting XML elements by their logical occurrence

查看:75
本文介绍了按逻辑事件排序XML元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我试图按逻辑事件对XML(子)元素进行排序。假设文件中有以下xml元素:



Hi,

I am trying to sort XML (child) elements by their logical occurrence. Assume there are following xml elements in a file:

<row id="1">
	<line1>value</line1>
	<line3>value</line3>
</row>
<row id="2">
	<line1>value</line1>
	<line2>value</line2>
	<line3>value</line3>
</row>
<row id="3">
	<line2>value</line2>
	<line4>value</line4>
</row>
<row id="4">
	<line2>value</line2>
	<line3>value</line3>
	<line4>value</line4>
</row>





结果我希望有一个唯一的子名称排序列表:line1,line2,line3,line4 。



lineXYZ仅用于插图;意思是:元素名称可能不同。因此,不能应用字母排序。

第二行line2是新的,但位于line3之前 - 与上一行相比 - 因此需要在line3之前排名。

在第3行中,line4在以前的条目中不存在,并且必须在末尾追加。



有人知道如何实现这一目标C#?

非常感谢!



我的尝试:



到目前为止,unfortuantely codeproject和google确实没有提供任何解决方案。



As result I would like to have an unique sorted list of child names: "line1, line2, line3, line4".

"lineXYZ" are only for illustrations; means: the element names could be different. Therefore an alphabetical sorting can not be applied.
In 2nd row "line2" is new but located before "line3" - compared to previous row - and needs therefore ranked before "line3".
In 3rd row "line4" does not exists in previous entries and has to append at the end.

Has someone any idea how this can be achieved in C#?
Many thanks!

What I have tried:

Unfortuantely codeproject nor google did provide any solution so far.

推荐答案

未经测试,但是这样:

Not tested, but something like this:
var listChilds = new List<string>;

using (XmlReader reader = XmlReader.Create("test.xml"))
{
	while (reader.Read())
	{
		if (reader.IsStartElement())
		{
			if (!listChilds.contains(reader.Name))
				listChilds.Add(reader.Name);
		}
	}
}


我建​​立了自己的解决方案,并希望将其分享给其他人,因为没有人能够提供一个。



1.我必须通过每个'row'元素解析XML以获取子节点:

I built my own solution and want to share it to others as nobody was able to provide one.

1. I have to parse the XML by each 'row' element to get the child nodes:
Dim lst As List(Of String) = Nothing ' use as list to store results
For Each c As XmlNode In n.ChildNodes ' loop throw each 'row' element
    Dim l As New List(Of String)
    For Each ch As XmlNode In c.ChildNodes ' loop throw each child of 'row'
        If ch.NodeType = XmlNodeType.Element Then l.Add(ch.Name) ' store element in list
    Next
    Merge(lst, l) ' merge both lists
Next

For Each s As String In lst
    Debug.Print(s)
Next





2.方法'Merge'完成工作 - 它检查第二个列表的每个元素是否已经在第一个列表中;如果不是:它试图在第一个列表中找到第二个列表的下一个值;可以找到现有值的值,它将第二个列表的值插入第一个索引,否则将其添加到第一个列表的末尾:



2. The method 'Merge' does the job - it checks if each element of 2nd list is already in 1st list; if not: it tries to find one of the next values of 2nd list in 1st list; of an existing value could be found, it inserts the value of 2nd list at index of 1st otherwise it adds it to end of 1st list:

Sub Merge(ByRef a As List(Of String), b As List(Of String))
    If a Is Nothing OrElse a.Count = 0 Then a = b

    ' check each item of 2nd list if exists in first list
    For i As Integer = 0 To b.Count - 1
        If Not a.Contains(b(i)) Then
            Dim index As Integer = -1

            ' check if one of the next items are already in first list
            For j As Integer = i + 1 To b.Count - 1
                If a.Contains(b(j)) Then
                    ' use index to append new value before existing value
                    index = a.IndexOf(b(j))
                    Exit For
                End If
            Next

            If Not index = -1 Then
                a.Insert(index, b(i)) ' append new value at index
            Else
                a.Add(b(i)) ' add value at end of list
            End If
        Else
            ' value exists already in first list; do nothing
        End If
    Next
End Sub





结果是正确顺序的预期列表:

line1

line2

line3

line4



The result is the expected list in correct order:
line1
line2
line3
line4


这篇关于按逻辑事件排序XML元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆