Hi I am Appending new Elements into XML file which is creating additional xmlns attribute and cannot be removed. i tried most of the common options
here's my code
This Add's new Entrys to the XML file
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
Above code adds uncessary xmlns which cannot be removed ......atleast easily
Here's the code i have tried to remove but not worked so far (its not complete function for simplicity)
' 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
here's the XML config file with two new entrys
<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" />
</depende开发者_如何转开发ntAssembly>
</assemblyBinding>
</runtime>
The XML that you're creating in your string literal has no namespace declared on the elements. The element you're appending it to belongs to a namespace. The parser is correct in adding xmlns=""
to the new elements - if it didn't, it would be changing their namespace.
To fix this, add a namespace declaration to the XML elements you're adding, declaring them to be in the same namespace as the document you're adding them to. Change:
<dependentAssembly>
to:
<dependentAssembly xmlns="urn:schemas-microsoft-com:asm.v1">
When you do this, the parser won't need to put an xmlns
declaration on the elements, because it will see that they already belong to the namespace that the parent element is in.
One other thing: look up VB.Net's support for XML literals.
精彩评论