Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
Hi,

sample code is,

<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Version="V1.0"></Root>


In this I need to remove xsd namespace.

Required:

<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  Version="V1.0"></Root>


I am using serialization.

How to achieve this in c# ?

Thanks

What I have tried:

 XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
//                ns.Add("xsd", "");
//                ns.Add("", "");


If I give prefix as empty , xsd and xsi both namespaces are gone.
if I give prefix as xsd, it throws exception
Posted
Updated 12-Dec-17 22:11pm
v2
Comments
F-ES Sitecore 13-Dec-17 4:23am    
Just do a string.Replace on the raw xml and replace the text you want gone with "". However I suspect you are trying to solve the wrong problem and trying to get rid of that text rather than getting your code to work with it.
vksvpp 13-Dec-17 4:49am    
this is required while generating xml

1 solution

You can simple split the XML string by blank and remove the part with "xmlns:xsd":

C#
string strXml = "<Root xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' Version='V1.0'></Root>";
string[] ar = strXml.Split(' ');
string strResult = string.Empty;
foreach (string str in ar)
{
   if (!str.Contains("xmlns:xsd"))
       strResult  += str + " ";
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900