Archive for the ‘XML’ Category

Searching Xml Element in any namespace using vb.net


If you know the namespaces that is in the xml document you can use XmlNamespaceManager;

  1. Dim xmlDoc As New XmlDocument()
  2. xmlDoc.Load(Path.Combine(mDirectory, name + "\" + name + ".vbproj"))          '"
  3.  
  4. Dim nsmgr As New XmlNamespaceManager(xmlDoc.NameTable)
  5. nsmgr.AddNamespace("msb", "http://schemas.microsoft.com/developer/msbuild/2003")
  6.  
  7. Dim xpath As String = "/msb:Project/msb:PropertyGroup/msb:ProjectGuid"
  8. Dim value As Object = xmlDoc.SelectNodes(xpath, nsmgr)

else you can use the general XPath syntax

//*[local-name() = 'ProjectGuid']

Source

No Comments


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


How to detemine namespaces used by the Xml Document

I have been browsing on the web on how to extract / determine the namespaces in the xml document given that the xml documetns are coming from different clients and there’s no standard content format, it took me a lot of days browsing to find the answer but no luck, what i found was only how to add/remove. So, what i did was experimenting / exploring the XmlDocument Object on my own, and fortunately, I found it.

Here’s the sample script on vb.net

‘Declare a variables

Dim nm as XmlNamespaceManager

Dim doc as new XmlDocument()

Dim root as XmlNode

doc.load(“c:\xml\test.xml”) ‘ xml path

nm = new XmlNamespaceManager(doc.NameTable);

Here’s the tricky part, you need to extract the attributes of the root document and iterate them.

root = doc.DocumentElement  ’get the root document

for i as integer =0 to root.Attributes.count -1

if roo.Attributes(i).Prefix.tolower() = “xmlns” then   ‘check if namespace

nm.AddNamespace (roo.Attributes(i).LocalName, roo.Attributes(i).Value)

endif

next

That’s it..

, ,

No Comments


Sum with empty element on XPath using .Net

When using sum function on XPath using .net you will get errors “Nan” if the node is empty because it use for numbers only. So, you need to select the nodes/elements that are not empty.

sum(//Quantity[node()])

, ,

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


How to Insert Xml Node from One document to another XML Document in vb.net

If you want to insert xmlnode from one document to another, you have to import the xmlnode. 

dim xmlDoc01 as XMLDocument

dim xmlDoc02 as XMLDocument

private sub InsertNod()  ’insert xmlnode from xmlDoc01 to xmlDoc02

{

xmldoc01.AppendChild(xmlDoc01.ImportNode(xmlDoc01.ChildNodes(0), true))

}

, , ,

No Comments



SetPageWidth