【问题标题】:VB.net XML Remove Every Second Element by Tag NameVB.net XML 按标签名称删除每隔一个元素
【发布时间】:2018-02-08 13:20:47
【问题描述】:

我正在使用以下 XML 示例:

<order>
    <name></name>
    <etc></etc>
    <etc></etc>
    <components>
        <component>
            <sub></sub>
        </component>
        <component>
            <sub></sub>
        </component>
        <component>
            <sub></sub>
        </component>
        <component>
            <sub></sub>
        </component>
    </components>
</order>

我正在尝试选择并删除每一秒 &lt;component&gt;,它是 vb.net 中的 subs。

以下是我目前得到的:

'make xmldocument object to hold xml string'
Dim XMLDoc As New XMLDocument()
XMLDoc.LoadXML(strXMLContent)
'get all elements named component'
Dim componentList As XmlNodeList = XMLDoc.GetElementsByTagName("Component")
Dim c As Integer
Dim i As Integer = 1
'for every component element add i to itself to only get seconds'
'and if on a second element, remove it and all sub elements inside it'
For c = 0 To componentList.Count - 1
    i += i
    If i > 2 Then
        componentList(c).ParentNode.RemoveChild(componentList(c))
    End If
Next c
'output to a string to see results'
x &= XMLDoc.DocumentElement.OuterXml

我遇到的问题是,当我运行此程序时,在调用实际 RemoveChild() 的行上得到“Object reference not set to an instance of an object”。

【问题讨论】:

  • 您能描述一下您遇到的问题吗?从您的问题中不清楚出了什么问题。
  • 现在我在运行它时得到“对象引用未设置为对象的实例”。我不确定在删除孩子的那一行应该使用什么逻辑。
  • 您应该在问题中直接包含任何信息,因为有些人可能会错过 cmets。这次我为你编辑了问题。这不是我的专业领域,我只是在审核您的问题(所有首次问题都已审核)所以现在看来​​这是一个很好的问题,我将把它留给专家来回答:)

标签: xml vb.net


【解决方案1】:

我的问题在于两件事,我如何添加我的 i 整数,以及 if 语句结构,当组合时,我删除的 componentList(c) 确实为空。以下是我的工作修复。

'make xmldocument object to hold xml string'
 Dim XMLDoc As New XMLDocument()
 XMLDoc.LoadXML(strXMLContent)
'get all elements named component'
 Dim componentList As XmlNodeList = XMLDoc.GetElementsByTagName("Component")
 Dim c As Integer
 Dim i As Integer = 0
'for every "Component" element add 1 to i - remove every second by checking if i is modular'
 For c = 0 To componentList.Count - 1
     i += 1
     If Not i Mod 2 Then
         componentList(c).ParentNode.RemoveChild(componentList(c))
     End If
 Next c
'output to a string to see results'
 x &= XMLDoc.DocumentElement.OuterXml

【讨论】:

    【解决方案2】:

    试试这个:

    Private Function GetRidOfEveryOtherComponent(ByVal strXML As String) As String
        'make xmldocument object to hold xml string'
        Dim XMLDoc As New XmlDocument()
        XMLDoc.LoadXml(strXML)
        'get every other component item
        Dim componentList As XmlNodeList = XMLDoc.SelectNodes("order/components/component[position() mod 2 = 1]")
        For c As Integer = 0 To componentList.Count - 1
            componentList(c).ParentNode.RemoveChild(componentList(c))
        Next c
        'output to a string to see results'
        Return XMLDoc.DocumentElement.OuterXml
    End Function
    

    这个很酷的 mod 功能是由 Paul 提供的,这里:Selecting every other node uing XPATH

    【讨论】:

    • 我的代码中的实际完整路径使用“soap:Envelope/soap:Body/SubmitOrder/Order/Components/Component[position() mod 2 = 1]”,这会产生此错误 - System.Xml .XPath.XPathException:需要命名空间管理器或 XsltContext。此查询具有前缀、变量或用户定义的函数 - 我知道我需要添加一个命名空间,但是对于哪一部分?
    猜你喜欢
    • 2012-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-02
    • 1970-01-01
    • 2020-09-28
    • 2018-07-06
    • 2016-10-07
    相关资源
    最近更新 更多