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;

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  3.   <Item>
  4.     <Code xmlns="http://schema.peters.com/doc_353/1/Types">0174587</Code>
  5.     <AltCode xmlns="http://schema.peters.com/doc_353/1/Types">014717</AltCode>
  6.     <supplier xmlns="http://schema.peters.com/doc_353/1/Types">019172</supplier>
  7.     <ID xmlns="http://schema.peters.com/doc_353/1/Types" />
  8.     <type3 xmlns="http://schema.peters.com/doc_353/1/Types">
  9.       <AltCode />
  10.       <main>false</main>
  11.     </type3>
  12.     <status xmlns="http://schema.peters.com/doc_353/1/Types">Some state</status>
  13.   </Item>
  14. </Root>
  1. Using LINQ to XML
    1. static XElement stripNS(XElement root) {
    2.  return new XElement( root.Name.LocalName, root.HasElements ? root.Elements().Select(el => stripNS(el)) : (object)root.Value );
    3. }
    4.  
    5. static void Main() {
    6.  var xml = XElement.Parse(@"<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    7.  <Item>
    8.    <Code xmlns="http://schema.peters.com/doc_353/1/Types">0174587</Code>
    9.    <AltCode xmlns="http://schema.peters.com/doc_353/1/Types">014717</AltCode>
    10.    <supplier xmlns="http://schema.peters.com/doc_353/1/Types">019172</supplier>
    11.    <ID xmlns="http://schema.peters.com/doc_353/1/Types" />
    12.    <type3 xmlns="http://schema.peters.com/doc_353/1/Types">
    13.      <AltCode />
    14.      <main>false</main>
    15.    </type3>
    16.    <status xmlns="http://schema.peters.com/doc_353/1/Types">Some state</status>
    17.  </Item>
    18. </Root>");
    19.  Console.WriteLine(stripNS(xml));
    20. }
    1.   static XElement stripNS(XElement root)
    2.     {
    3.         XElement res = new XElement(
    4.             root.Name.LocalName,
    5.             root.HasElements ?
    6.                 root.Elements().Select(el => stripNS(el)) :
    7.                 (object)root.Value
    8.         );
    9.  
    10.         res.ReplaceAttributes(
    11.             root.Attributes().Where(attr => (!attr.IsNamespaceDeclaration)));
    12.  
    13.         return res;
    14.     }
    1. Public Function RemoveAllNamespaces(ByVal element As XElement) As XElement
    2.         If element.HasElements Then
    3.             Dim cleanElement = RemoveAllNamespaces(New XElement(element.Name.LocalName, element.Attributes))
    4.             cleanElement.Add(element.Elements.Select(Function(el) RemoveAllNamespaces(el)))
    5.             Return cleanElement
    6.         Else
    7.             Dim allAttributesExceptNamespaces = element.Attributes.Where(Function(attr) Not attr.IsNamespaceDeclaration)
    8.             element.ReplaceAttributes(allAttributesExceptNamespaces)
    9.             Return element
    10.         End If
    11.  
    12.     End Function
  2. Using XLT
    1. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  
    2.  <xsl:output method="xml" indent="no" encoding="UTF-8"/>  
    3.  <xsl:template match="/|comment()|processing-instruction()">    
    4.   <xsl:copy>      
    5.    <xsl:apply-templates/>    
    6.   </xsl:copy>  
    7.  </xsl:template>  
    8.  <xsl:template match="*">    
    9.   <xsl:element name="{local-name()}">      
    10.    <xsl:apply-templates select="@*|node()"/>    
    11.   </xsl:element>  
    12.  </xsl:template>  
    13.  <xsl:template match="@*">    
    14.   <xsl:attribute name="{local-name()}">      
    15.    <xsl:value-of select="."/>    
    16.   </xsl:attribute>  
    17.  </xsl:template>
    18. </xsl:stylesheet>
  3. Using Regular expression
    1. string XMLPattern = "xmlns=\\\".+\\\"";
    2. Regex regXML = new Regex(pattern);
    3. string XMLInput = FancyMethodThatPutsXMLIntoString();
    4. 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

,

No Comments


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:

schema_error

,

No Comments


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”)

, , , ,

No Comments



SetPageWidth