0
嗨,我將新元素添加到XML文件中,該文件正在創建其他xmlns屬性並且無法刪除。我想大多數的常用選項從xml文件中移除xmlns
這裏是我的代碼
這個附加的新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不能被刪除...... ATLEAST容易
下面的代碼我試圖刪除卻遠沒有那麼工作(其爲簡單起見,不完整的功能)
' 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
這裏有兩個新的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" />
</dependentAssembly>
</assemblyBinding>
</runtime>
沒有,沒有工作?它把而不只是 –
Gauls
2010-10-04 11:10:39
在你的輸出中,'assemblyBinding'及其子'dependentAssembly'具有完全相同的名稱空間聲明嗎? – 2010-10-04 12:30:36
yes urn:schemas-microsoft-com:asm.v1 – Gauls 2010-10-04 14:27:49