2010-10-01 100 views
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> 

回答

0

,你創建你的字符串字面量有沒有上的元素聲明命名空間中的XML的XML配置文件。您將它附加到的元素屬於一個名稱空間。解析器在向新元素添加xmlns=""時是正確的 - 如果沒有,它將改變它們的命名空間。

若要解決此問題,請向您要添加的XML元素添加一個名稱空間聲明,並將它們聲明爲與您將它們添加到的文檔位於同一名稱空間中。變化:

<dependentAssembly> 

到:

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

當你這樣做時,解析器不會需要把xmlns聲明上的元素,因爲它會看到,他們已經屬於命名空間父元素英寸

另一件事:查找VB.Net的支持XML literals

+0

沒有,沒有工作?它把而不只是 Gauls 2010-10-04 11:10:39

+0

在你的輸出中,'assemblyBinding'及其子'dependentAssembly'具有完全相同的名稱空間聲明嗎? – 2010-10-04 12:30:36

+0

yes urn:schemas-microsoft-com:asm.v1 – Gauls 2010-10-04 14:27:49

相關問題