Archive for the ‘VB.Net’ 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 use Bitwise in Vb.Net
    Enum eStatusNumber
        One = 1
        Two = 2
        Three = 4
        Four = 16
        Five = 32
    End Enum

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Test(eStatusNumber.Five Or eStatusNumber.Three)
    End Sub

    Private Sub Test(ByVal com As eStatusNumber)
        If (com And eStatusNumber.One) = eStatusNumber.One Then
            MsgBox("One")
        End If
        If (com And eStatusNumber.Two) = eStatusNumber.Two Then
            MsgBox("Two")
        End If

        If (com And eStatusNumber.Three) = eStatusNumber.Three Then
            MsgBox("Three")
        End If
        If (com And eStatusNumber.Four) = eStatusNumber.Four Then
            MsgBox("Four")
        End If

        If (com And eStatusNumber.Five) = eStatusNumber.Five Then
            MsgBox("Five")
        End If

    End Sub

VB.Net code can be downloaded here "EnumBitWise Sample".
Reference 1
Reference 2

				

, ,

No Comments


How to open the windows explorer programmatically in .net


How to open the windows explorer using vb.net

  1. System.Diagnostics.Process.Start("explorer.exe", "c:\tmp")

, , ,

No Comments


How to convert .net Form to Image
  1. Public Class Form1
  2.  
  3.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  4.         CreateBitmap(Me)
  5.     End Sub
  6.  
  7.     Private Sub CreateBitmap(ByVal con As Control)
  8.         'Dim g As Graphics = Me.CreateGraphics()
  9.  
  10.         Dim b As Bitmap = New Bitmap(Me.Width, Me.Height)
  11.         Me.DrawToBitmap(b, New Rectangle(0, 0, Me.Width, Me.Height))
  12.  
  13.         b.Save("test.jpg", Imaging.ImageFormat.Jpeg)
  14.         b.Dispose()
  15.         'g.Dispose()
  16.     End Sub
  17. End Class

, ,

No Comments


Enum: How to get the underlying value given the name

Use Parse method of the enum to get the value:

Enum x
a = 1
b = 2
End Enum

dim e as x

e = [enum].Parse(GetType(x), “a”)

,

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


InvalidOperationException : There was an error in XML document

This error occurred when there is an invalid character on the data. Before adding/saving to the datatable you need to replace the invalid character. The known invalid character is the null.

To replace it in vb.net
dim mData As string
Strings.Replace(mData, vbNullChar, “”)

No Comments


How to Convert Arraylist to String using .Net
  1. Public Overloads Function ArrayListToString(ByVal ar As System.Collections.ArrayList) As String
  2.  Return ArrayListToString(ar, ","C)
  3. End Function
  4.  
  5. Public Overloads Function ArrayListToString(ByVal ar As System.Collections.ArrayList, ByVal delim As Char) As String
  6.  Return ArrayListToString(ar, delim.ToString)
  7. End Function
  8.  
  9. Public Overloads Function ArrayListToString(ByVal ar As System.Collections.ArrayList, ByVal delim As String) As String
  10.  Return String.Join(delim, CType(ar.ToArray(GetType(String)), String()))
  11. End Function

, , ,

No Comments


How to convert string to Hex
  1. Function HexToDec(ByVal value As String) As Long
  2.     ' we just need a call to the Convert.ToInt64 static method
  3.     Return Convert.ToInt64(value, 16)
  4. End Function

,

No Comments



SetPageWidth