2008-09-16 81 views
6

對於任意屬性,XmlElement.Attributes.Remove *方法正常工作,從而導致從XmlDocument.OuterXml屬性中刪除已刪除的屬性。然而Xmlns屬性是不同的。這裏有一個例子:如何使用.NET XML API刪除xmlns屬性

XmlDocument doc = new XmlDocument(); 
doc.InnerXml = @"<Element1 attr1=""value1"" xmlns=""http://mynamespace.com/"" attr2=""value2""/>"; 
doc.DocumentElement.Attributes.RemoveNamedItem("attr2"); 
Console.WriteLine("xmlns attr before removal={0}", doc.DocumentElement.Attributes["xmlns"]); 
doc.DocumentElement.Attributes.RemoveNamedItem("xmlns"); 
Console.WriteLine("xmlns attr after removal={0}", doc.DocumentElement.Attributes["xmlns"]); 

得到的輸出

xmlns attr before removal=System.Xml.XmlAttribute 
xmlns attr after removal= 
<Element1 attr1="value1" xmlns="http://mynamespace.com/" /> 

屬性似乎從屬性集合中刪除,但它不會從XmlDocument.OuterXml刪除。 我想這是因爲這個屬性的特殊含義。

問題是如何使用.NET XML API刪除xmlns屬性。 很明顯,我可以從字符串表示形式中刪除屬性,但我想知道是否可以使用API​​來做同樣的事情。

@編輯:我在談論.NET 2.0。

+0

我剛剛遇到了這個問題。好找! – 2009-10-23 21:41:21

回答

2

.NET DOM API不支持修改元素的名稱空間,這正是您本質上想要做的。所以,爲了解決你的問題,你必須以這樣或那樣的方式構建一個新的文檔。您可以使用相同的.NET DOM API並創建一個新元素而不指定其名稱空間。或者,您可以創建一個XSLT樣式表,將原來的「命名空間」文檔轉換爲其中元素不受命名空間限定的新文檔。

+0

我不確定,但只要沒有肯定的答案,我相信這是真的。 – axk 2008-09-17 19:18:00

0

是的,因爲它是ELEMENT名稱,所以不能明確地將其刪除。使用XmlTextWriter的WriteStartElement和WirteStartAttribute,並用空白替換屬性可能會完成工作。

我正在查看它。將更新。

1

是不是應該刪除命名空間?

XmlNamespaceManager mgr = new XmlNamespaceManager("xmlnametable"); 
mgr.RemoveNamespace("prefix", "uri"); 

但無論如何,在這裏切線的的XElement,和的XDocument從System.Xml.Linq的命名空間XNameSpace類(.NET 3.0)是一個更好的很多較老的XmlDocument模型。搏一搏。我上癮了。

+0

感謝您的建議。一定會試一試。 – axk 2008-09-17 19:16:57

0

我們可以將xml轉換爲字符串,從該字符串中移除xmlns,然後使用此字符串創建另一個XmlDocument,該字符串將不具有該名稱空間。

1

我看到了這個線程中的各種選項,來解決我自己的解決方案,用於刪除xml中的xmlns屬性。這是正常工作,並沒有問題:

'Remove the Equifax/Transunian/Experian root node attribute that have xmlns and load xml without xmlns attributes. 
If objXMLDom.DocumentElement.NamespaceURI <> String.Empty Then 
    objXMLDom.LoadXml(objXMLDom.OuterXml.Replace(objXMLDom.DocumentElement.NamespaceURI, "")) 
    objXMLDom.DocumentElement.RemoveAllAttributes() 
    ResponseXML = objXMLDom.OuterXml 
End If 

有沒有必要做任何事情來從xml中刪除xmlns。

2

非常感謝Ali Shah,這個主題完美地解決了我的問題! 這裏有一個C#轉換:

var dom = new XmlDocument(); 
     dom.Load("C:/ExampleFITrade.xml)); 
     var loaded = new XDocument(); 
     if (dom.DocumentElement != null) 
      if(dom.DocumentElement.NamespaceURI != String.Empty) 
      { 
       dom.LoadXml(dom.OuterXml.Replace(dom.DocumentElement.NamespaceURI, "")); 
       dom.DocumentElement.RemoveAllAttributes(); 
       loaded = XDocument.Parse(dom.OuterXml); 
      } 
1
public static string RemoveXmlns(string xml) 
{ 
    //Prepare a reader 
    StringReader stringReader = new StringReader(xml); 
    XmlTextReader xmlReader = new XmlTextReader(stringReader); 
    xmlReader.Namespaces = false; //A trick to handle special xmlns attributes as regular 
    //Build DOM 
    XmlDocument xmlDocument = new XmlDocument(); 
    xmlDocument.Load(xmlReader); 
    //Do the job 
    xmlDocument.DocumentElement.RemoveAttribute("xmlns"); 
    //Prepare a writer 
    StringWriter stringWriter = new StringWriter(); 
    XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter); 
    //Optional: Make an output nice ;) 
    xmlWriter.Formatting = Formatting.Indented; 
    xmlWriter.IndentChar = ' '; 
    xmlWriter.Indentation = 2; 
    //Build output 
    xmlDocument.Save(xmlWriter); 
    return stringWriter.ToString(); 
} 
+2

對你提出的答案有一些敘述性描述會有幫助。 – Rob 2013-01-11 22:37:24

0

這裏是我的vb.net傢伙的解決方案!

Dim pathXmlTransformado As String = "C:\Fisconet4\process\11790941000192\2015\3\28\38387-1\38387_transformado.xml" 
    Dim nfeXML As New XmlDocument 
    Dim loaded As New XDocument 

    nfeXML.Load(pathXmlTransformado) 

    nfeXML.LoadXml(nfeXML.OuterXml.Replace(nfeXML.DocumentElement.NamespaceURI, "")) 
    nfeXML.DocumentElement.RemoveAllAttributes() 

    Dim dhCont As XmlNode = nfeXML.CreateElement("dhCont") 
    Dim xJust As XmlNode = nfeXML.CreateElement("xJust") 
    dhCont.InnerXml = 123 
    xJust.InnerXml = 123777 

    nfeXML.GetElementsByTagName("ide")(0).AppendChild(dhCont) 
    nfeXML.GetElementsByTagName("ide")(0).AppendChild(xJust) 

    nfeXML.Save("C:\Fisconet4\process\11790941000192\2015\3\28\38387-1\teste.xml")