可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
XmlElement.Attributes.Remove* methods are working fine for arbitrary attributes resulting in the removed attributes being removed from XmlDocument.OuterXml property. Xmlns attribute however is different. Here is an example:
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"]);
The resulting output is
xmlns attr before removal=System.Xml.XmlAttribute
xmlns attr after removal=
<Element1 attr1="value1" xmlns="http://mynamespace.com/" />
The attribute seems to be removed from the Attributes collection, but it is not removed from XmlDocument.OuterXml.
I guess it is because of the special meaning of this attribute.
The question is how to remove the xmlns attribute using .NET XML API.
Obviously I can just remove the attribute from a String representation of this, but I wonder if it is possible to do the same thing using the API.
@Edit: I'm talking about .NET 2.0.
回答1:
.NET DOM API doesn't support modifying element's namespace which is what you are essentially trying to do. So, in order to solve your problem you have to construct a new document one way or another. You can use the same .NET DOM API and create a new element without specifying its namespace. Alternatively, you can create an XSLT stylesheet that transforms your original "namespaced" document to a new one in which the elements will be not namespace-qualified.
回答2:
I saw the various options in this thread and come to solve my own solution for removing xmlns attributes in xml. This is working properly and has no issues:
'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
There is no need to do anything else to remove xmlns from xml.
回答3:
Many thanks to Ali Shah, this thread solved my problem perfectly!
here's a C# conversion:
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);
}
回答4:
Wasn't this supposed to remove namespaces?
XmlNamespaceManager mgr = new XmlNamespaceManager("xmlnametable");
mgr.RemoveNamespace("prefix", "uri");
But anyway on a tangent here, the XElement, XDocument and XNameSpace classes from System.Xml.Linq namespace (.Net 3.0) are a better lot than the old XmlDocument model. Give it a go. I am addicted.
回答5:
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();
}
回答6:
Yes, because its an ELEMENT name, you can't explicitly remove it. Using XmlTextWriter's WriteStartElement and WirteStartAttribute, and replacing the attribute with empty spaces will likely to get the job done.
I'm checking it out now. will update.
回答7:
Maybe trough the XmlNamespaceManager ? http://msdn.microsoft.com/en-us/library/system.xml.xmlnamespacemanager.removenamespace.aspx but it's just a guess.
回答8:
We can convert the xml to a string, remove the xmlns from that string, and then create another XmlDocument using this string, which will not have the namespace.
回答9:
here is my solution on vb.net guys!
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")