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;
-
Dim xmlDoc As New XmlDocument()
-
xmlDoc.Load(Path.Combine(mDirectory, name + "\" + name + ".vbproj")) '"
-
-
Dim nsmgr As New XmlNamespaceManager(xmlDoc.NameTable)
-
nsmgr.AddNamespace("msb", "http://schemas.microsoft.com/developer/msbuild/2003")
-
-
Dim xpath As String = "/msb:Project/msb:PropertyGroup/msb:ProjectGuid"
-
Dim value As Object = xmlDoc.SelectNodes(xpath, nsmgr)
else you can use the general XPath syntax
//*[local-name() = 'ProjectGuid']
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
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
How to open the windows explorer programmatically in .net
How to open the windows explorer using vb.net
-
System.Diagnostics.Process.Start("explorer.exe", "c:\tmp")
How to convert .net Form to Image
-
Public Class Form1
-
-
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
-
CreateBitmap(Me)
-
End Sub
-
-
Private Sub CreateBitmap(ByVal con As Control)
-
'Dim g As Graphics = Me.CreateGraphics()
-
-
Dim b As Bitmap = New Bitmap(Me.Width, Me.Height)
-
Me.DrawToBitmap(b, New Rectangle(0, 0, Me.Width, Me.Height))
-
-
b.Save("test.jpg", Imaging.ImageFormat.Jpeg)
-
b.Dispose()
-
'g.Dispose()
-
End Sub
-
End Class
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”)
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 Convert Arraylist to String using .Net
Posted by: admin in Array, String Manipulation, VB.Net on October 28th, 2009
-
Public Overloads Function ArrayListToString(ByVal ar As System.Collections.ArrayList) As String
-
Return ArrayListToString(ar, ","C)
-
End Function
-
-
Public Overloads Function ArrayListToString(ByVal ar As System.Collections.ArrayList, ByVal delim As Char) As String
-
Return ArrayListToString(ar, delim.ToString)
-
End Function
-
-
Public Overloads Function ArrayListToString(ByVal ar As System.Collections.ArrayList, ByVal delim As String) As String
-
Return String.Join(delim, CType(ar.ToArray(GetType(String)), String()))
-
End Function
How to convert string to Hex
Posted by: admin in .NET, String Manipulation, VB.Net on September 8th, 2009
-
Function HexToDec(ByVal value As String) As Long
-
' we just need a call to the Convert.ToInt64 static method
-
Return Convert.ToInt64(value, 16)
-
End Function