从xml文件中删除xmlns

时间:2010-10-01 10:09:45

标签: .net xml vb.net

您好我将新元素附加到XML文件中,该文件正在创建其他xmlns属性,无法删除。我尝试了大多数常见选项

这是我的代码

将Add的新Entrys添加到XML文件

Private Sub AddNewElement(ByRef FileInfo As System.IO.FileInfo)

        Dim bln As Boolean = False
        Dim XmlDoc As System.Xml.XmlDocument = Nothing
        Dim OAssemblyInfo As AssemblyInfo = Nothing
        Dim NodeDependentAssembly As System.Xml.XmlNode
        Dim NodeAssemblyIdentity As System.Xml.XmlNode = Nothing
        Dim NodeBindingRedirect As System.Xml.XmlNode = Nothing
        Dim nodelist As System.Xml.XmlNodeList

        Try
            Dim cr As String = Environment.NewLine
            Dim newElement As String = _
           "<dependentAssembly>" & cr & _
           "<assemblyIdentity name=" & """" & "ABCEntities" & """" & " publicKeyToken=" & """" & "21f532fe36bf9cd6" & """" & " culture=" & """" & "neutral" & """" & " />" & cr & _
           "<bindingRedirect oldVersion=" & """" & "1.0.0.0-65535.65535.65535.65535" & """" & " newVersion=" & """" & "7.136.13.0" & """" & " />" & cr & _
           "<codeBase version=" & """" & "7.136.13.0" & """" & " href=" & """" & "file:///C:\Program Files\ABC\ABCEntities.dll" & """" & " />" & cr & _
           "</dependentAssembly>" & cr & _
          "<dependentAssembly>" & cr & _
           "<assemblyIdentity name=" & """" & "ABCProcesses" & """" & " publicKeyToken=" & """" & "21f532fe36bf9cd6" & """" & " culture=" & """" & "neutral" & """" & " />" & cr & _
           "<bindingRedirect oldVersion=" & """" & "1.0.0.0-65535.65535.65535.65535" & """" & " newVersion=" & """" & "7.136.13.0" & """" & " />" & cr & _
           "<codeBase version=" & """" & "7.136.13.0" & """" & " href=" & """" & "file:///C:\Program Files\ABC\ABCProcesses.dll" & """" & " />" & cr & _
          "</dependentAssembly>" & cr


            ' Load the config file into the XML DOM.
            XmlDoc = New System.Xml.XmlDocument
            XmlDoc.Load(FileInfo.FullName)

            For Each NodeDependentAssembly In XmlDoc.Item("configuration").Item("runtime").Item("assemblyBinding")
                ' Skip any comments.
                If NodeDependentAssembly.Name = "dependentAssembly" Then

                    If NodeDependentAssembly.HasChildNodes = True Then

                        nodelist = NodeDependentAssembly.ChildNodes
                        NodeAssemblyIdentity = nodelist.ItemOf(0)


                        Select Case NodeAssemblyIdentity.Attributes.GetNamedItem("name").Value

                            Case "ABCEntities"
                                bln = True

                            Case "ABCProcesses"
                                bln = True
                        End Select
                    End If
                End If
            Next



            If bln = False Then

                Dim docFrag As XmlDocumentFragment = XmlDoc.CreateDocumentFragment()


                docFrag.InnerXml = newElement
                                    Dim root As XmlNode = XmlDoc.Item("configuration").Item("runtime").Item("assemblyBinding")

                root.AppendChild(docFrag)

                ' Save the Xml.
                XmlDoc.Save(FileInfo.FullName)


            End If

        Catch ex As Exception
            Throw
        Finally
            XmlDoc = Nothing
        End Try
    End Sub

上面的代码添加了无法删除的uncessary xmlns ......至少很容易

这是我试图删除的代码,但到目前为止还没有完成(为简单起见,它的功能不完整)

   ' Load the config file into the XML DOM.
            XmlDoc = New System.Xml.XmlDocument
            XmlDoc.Load(FileInfo.FullName)


            For Each NodeDependentAssembly In XmlDoc.Item("configuration").Item("runtime").Item("assemblyBinding")

                ' Skip any comments.
                If NodeDependentAssembly.Name = "dependentAssembly" Then

                    If NodeDependentAssembly.Attributes.Count > 0 Then
                        NodeDependentAssembly.Attributes.RemoveAll()
                        NodeDependentAssembly.OuterXml.Replace("xmlns", "")
                        NodeDependentAssembly.Attributes.RemoveAll()
                    End If

这是带有两个新托管的XML配置文件

 <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly xmlns=""> -----------remove xmlns from here
               <assemblyIdentity name="ABCEntities" publicKeyToken="21f532fe36bf9cd6" culture="neutral" />
        <bindingRedirect oldVersion="1.0.0.0-65535.65535.65535.65535 " newVersion="7.136.13.0" />
        <codeBase version="7.136.13.0" href="file:///C:\Program Files\ABC\ABCEntities.dll" />
      </dependentAssembly>
      <dependentAssembly xmlns=""> -----------remove xmlns from here
        <assemblyIdentity name="ABCProcesses" publicKeyToken="21f532fe36bf9cd6" culture="neutral" />
        <bindingRedirect oldVersion="1.0.0.0-65535.65535.65535.65535 " newVersion="7.136.13.0" />
        <codeBase version="7.136.13.0" href="file:///C:\Program Files\ABC\ABCProcesses.dll" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

1 个答案:

答案 0 :(得分:0)

您在字符串文字中创建的XML没有在元素上声明名称空间。您要附加的元素属于命名空间。解析器在将xmlns=""添加到新元素时是正确的 - 如果没有,则会更改其命名空间。

要解决此问题,请向要添加的XML元素添加名称空间声明,声明它们与您要添加的文档位于同一名称空间中。变化:

<dependentAssembly>

为:

<dependentAssembly xmlns="urn:schemas-microsoft-com:asm.v1">

执行此操作时,解析器不需要在元素上放置xmlns声明,因为它会看到它们已经属于父元素所在的命名空间。

另一件事:查看VB.Net对XML literals的支持。