Posts Tagged ‘schema’
How to remove xml namespaces in .Net
There’s a lot of articles or forums regarding this issue of removing namespaces in xml. Here are a list of solutions compiled from other resources that I found useful. Pick and use based on your preferable solution.
Using a simple xml below as an example;
-
<?xml version="1.0" encoding="utf-8"?>
-
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
-
<Item>
-
<Code xmlns="http://schema.peters.com/doc_353/1/Types">0174587</Code>
-
<AltCode xmlns="http://schema.peters.com/doc_353/1/Types">014717</AltCode>
-
<supplier xmlns="http://schema.peters.com/doc_353/1/Types">019172</supplier>
-
<ID xmlns="http://schema.peters.com/doc_353/1/Types" />
-
<type3 xmlns="http://schema.peters.com/doc_353/1/Types">
-
<AltCode />
-
<main>false</main>
-
</type3>
-
<status xmlns="http://schema.peters.com/doc_353/1/Types">Some state</status>
-
</Item>
-
</Root>
- Using LINQ to XML
-
static XElement stripNS(XElement root) {
-
return new XElement( root.Name.LocalName, root.HasElements ? root.Elements().Select(el => stripNS(el)) : (object)root.Value );
-
}
-
-
static void Main() {
-
var xml = XElement.Parse(@"<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
-
<Item>
-
<Code xmlns="http://schema.peters.com/doc_353/1/Types">0174587</Code>
-
<AltCode xmlns="http://schema.peters.com/doc_353/1/Types">014717</AltCode>
-
<supplier xmlns="http://schema.peters.com/doc_353/1/Types">019172</supplier>
-
<ID xmlns="http://schema.peters.com/doc_353/1/Types" />
-
<type3 xmlns="http://schema.peters.com/doc_353/1/Types">
-
<AltCode />
-
<main>false</main>
-
</type3>
-
<status xmlns="http://schema.peters.com/doc_353/1/Types">Some state</status>
-
</Item>
-
</Root>");
-
Console.WriteLine(stripNS(xml));
-
}
-
static XElement stripNS(XElement root)
-
{
-
XElement res = new XElement(
-
root.Name.LocalName,
-
root.HasElements ?
-
root.Elements().Select(el => stripNS(el)) :
-
(object)root.Value
-
);
-
-
res.ReplaceAttributes(
-
root.Attributes().Where(attr => (!attr.IsNamespaceDeclaration)));
-
-
return res;
-
}
-
Public Function RemoveAllNamespaces(ByVal element As XElement) As XElement
-
If element.HasElements Then
-
Dim cleanElement = RemoveAllNamespaces(New XElement(element.Name.LocalName, element.Attributes))
-
cleanElement.Add(element.Elements.Select(Function(el) RemoveAllNamespaces(el)))
-
Return cleanElement
-
Else
-
Dim allAttributesExceptNamespaces = element.Attributes.Where(Function(attr) Not attr.IsNamespaceDeclaration)
-
element.ReplaceAttributes(allAttributesExceptNamespaces)
-
Return element
-
End If
-
-
End Function
-
- Using XLT
-
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
-
<xsl:output method="xml" indent="no" encoding="UTF-8"/>
-
<xsl:template match="/|comment()|processing-instruction()">
-
<xsl:copy>
-
<xsl:apply-templates/>
-
</xsl:copy>
-
</xsl:template>
-
<xsl:template match="*">
-
<xsl:element name="{local-name()}">
-
<xsl:apply-templates select="@*|node()"/>
-
</xsl:element>
-
</xsl:template>
-
<xsl:template match="@*">
-
<xsl:attribute name="{local-name()}">
-
<xsl:value-of select="."/>
-
</xsl:attribute>
-
</xsl:template>
-
</xsl:stylesheet>
-
- Using Regular expression
-
string XMLPattern = "xmlns=\\\".+\\\"";
-
Regex regXML = new Regex(pattern);
-
string XMLInput = FancyMethodThatPutsXMLIntoString();
-
string Results = regXML.Replace(XMLInput, "");
Note: The triple slashes serve to escape the escaping of the quotes for your regex formula. Technically the formula is xmlns=\”.+\” -
To follow the discussions regarding this issue, please visit the source at http://stackoverflow.com
The following text is not allowed in this context:
This exception occurs when there is an extra string / character outside the element of the schema,
example:

How to Create Schema from XML in .net
You will use the xsd.exe to create an schema from xml.
Process.start(“xsd.exe”, “xmlfile”)