I need to remove all namespaces from xml file shown after c sharp code (also been attached).
The problem is that I am getting the following error being returned by one of the functions
{System.Xml.XmlException: The prefix '' cannot be redefined from '' to 'http://sample.response.power.core.com' within the same start element tag.
at System.Xml.XmlWellFormedWriter.PushNamespaceExplic it(String prefix, String ns)
at System.Xml.XmlWellFormedWriter.WriteEndAttribute()
at System.Xml.Linq.ElementWriter.WriteStartElement(XE lement e)
at System.Xml.Linq.ElementWriter.WriteElement(XElemen t e)
at System.Xml.Linq.XElement.WriteTo(XmlWriter writer)
at System.Xml.Linq.XNode.GetXmlString(SaveOptions o)
at System.Xml.Linq.XNode.ToString()
}
Can somebody think of an alternative way of removing namespaces within an xml file or figure out the issue with my code please?
Code:
Expand|Select|Wrap|Line Numbers
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Xml;
- using System.IO;
- using System.Xml.Serialization;
- namespace ConsoleApplication2
- {
- class Program
- {
- public static string RemoveAllNamespaces(string xmlDocument)
- {
- XElement xmlDocumentWithoutNs = RemoveAllNamespaces(XElement.Parse(xmlDocument));
- return xmlDocumentWithoutNs.ToString();
- }
- //Core recursion function
- private static XElement RemoveAllNamespaces(XElement xmlDocument)
- {
- if (!xmlDocument.HasElements)
- {
- XElement xElement = new XElement(xmlDocument.Name.LocalName);
- xElement.Value = xmlDocument.Value;
- foreach (XAttribute attribute in xmlDocument.Attributes())
- xElement.Add(attribute);
- return xElement;
- }
- return new XElement(xmlDocument.Name.LocalName, xmlDocument.Elements().Select(el => RemoveAllNamespaces(el)));
- }
- static void Main(string[] args)
- {
- XmlDocument document = new XmlDocument();
- document.Load("C:\\temp\\xmlfile.xml");
- XmlNamespaceManager mgr = new XmlNamespaceManager(document.NameTable);
- mgr.AddNamespace("ns", "http://sample.stock.power.vno.com");
- XmlNodeList nodes = document.SelectNodes("/GetOrderMessagesResponse/ns:orderResponses/ns:OrderResponse", mgr);
- foreach (XmlNode n in nodes)
- {
- XmlDocument outdoc = new XmlDocument();
- XmlNode targetNode = outdoc.CreateElement("OrderResponse");
- targetNode = outdoc.ImportNode(n, true);
- targetNode.Attributes.RemoveAll();
- outdoc.ImportNode(targetNode, true);
- outdoc.AppendChild(targetNode);
- XmlSerializer serializer = new XmlSerializer(outdoc.GetType());
- XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
- ns.Add("","");
- StringWriter myWriter = new StringWriter();
- serializer.Serialize(myWriter, outdoc, ns);
- String temp = myWriter.ToString();
- Console.WriteLine(RemoveAllNamespaces(temp));
- Console.ReadKey();
- }
<?xml version="1.0" encoding="utf-8"?>
<PaymentReference xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Stock xsi:nil="true" xmlns="http://bloom.stock.power.vno.com" />
<Response xmlns="http://sample.stock.power.vno.com">
<Order>
<OrderDateTime>2012-09-16T09:28:10+01:00</OrderDateTime> <addressloc>XL11046560</addressloc>
<appReference xsi:nil="true" />
<buyerID>SN999</buyerID>
<cancellationReason xsi:nil="true" />
<caseReference xsi:nil="true" />
<cessationReason xsi:nil="true" />
<cssDatabaseCode xsi:nil="true" />
<currentSPName xsi:nil="true" />
<liners xsi:nil="true" />
<Notes>
<Comment xmlns="http://sample.document.power.vno.com"> <completionNo>No019</completionNo>
<message>Incorrect ID</message>
</Comment>
</Notes>
<newRcfOrCrServiceId xsi:nil="true" />
<notes xsi:nil="true" />
<numberOfOrders xsi:nil="true" />
<parentOrderRef xsi:nil="true" />
<postcode xsi:nil="true" />
<projectId xsi:nil="true" />
<serviceId xsi:nil="true" />
<Address>
<addressCode xsi:nil="true" xmlns="http://sample.response.power.core.com" /> <addressRegion xmlns="http://sample.response.power.core.com">NW01254</addressRegion>
<Nation xmlns="http://sample.response.power.core.com">United Kingdom</Nation>
<county xsi:nil="true" xmlns="http://sample.response.power.core.com" /> <organisationType xsi:nil="http://sample.response.power.core.com" /> </Address>
<status>
<created>2012-09-16T09:28:31+01:00</created> <Id>Core24</Id>
<operatorname>test_Name</operatorname>
</status>
<subOrderType>PROVIDE_GOLDEN</subOrderType> <telephoneNumber>01125412514</telephoneNumber> <validity>false</validity> <vatCode xsi:nil="true" />
</Order>
</Response>
</PaymentReference>