Hi,
Sorry if this is deemed to be a cross post (I also posted to the
microsoft.publi c.xml)
(Using .NET 2003, VB, C# on a Win2k PC)
I'm trying to retrieve nodes from an XML doc by name. I have found that the
following code, when translated into C# works perfectly, but not when used
in VB. In the example below the "applicatio n" node returns a value when
accessed through the C# code but not with the VB code.
If I add a prefix to the namespace in the source XML doc e.g. Change
<application xmlns="http://MyCompanyName.c o.uk/test/1.0.08/">
to <application xmlns:TEST="htt p://MyCompanyName.c o.uk/test/1.0.08/">
then it works fine in both languages.
Does anyone have experience of this problem ?
Below are the code samples from both languages... followed by the XML
excerpt
Thanks
Gary
VB Code --
****
Imports System
Imports System.Xml
Module Module1
Sub Main()
Try
Dim oXmlDoc As New Xml.XmlDocument
Dim sFileName As String = "04231ESWM00124 0.xml"
oXmlDoc.Load(sF ileName)
Dim nsmgr As XmlNamespaceMan ager = New
XmlNamespaceMan ager(oXmlDoc.Na meTable)
nsmgr.AddNamesp ace("clps",
"http://MyCompanyName.c o.uk/test/1.0.08")
nsmgr.AddNamesp ace("xsi",
"http://www.w3.org/2001/XMLSchema-instance")
nsmgr.AddNamesp ace("xsd", "http://www.w3.org/2001/XMLSchema")
nsmgr.AddNamesp ace("soap",
"http://schemas.xmlsoap .org/soap/envelope/")
Console.WriteLi ne("Def NS = " & nsmgr.DefaultNa mespace)
Dim oNode As Xml.XmlNode
oNode = oXmlDoc.SelectS ingleNode("//application", nsmgr)
If oNode Is Nothing Then
Console.WriteLi ne("The application object was not found")
Else
Console.WriteLi ne(oNode.InnerT ext)
End If
Finally
Console.WriteLi ne("Press a key to end")
Console.Read()
End Try
End Sub
and the C# version ****
using System;
using System.Xml;
namespace TestXML
{
class Class1
{
ry>
[STAThread]
static void Main(string[] args)
{
XmlDocument xmlRequest = new XmlDocument();
xmlRequest.Load (@"N:\04231ESWM 001240.xml");
XmlNamespaceMan ager nsMgr = new XmlNamespaceMan ager(xmlRequest .NameTable);
nsMgr.AddNamesp ace("clps","htt p://MyCompanyName.c o.uk/test/1.0.08/");
nsMgr.AddNamesp ace("xsi","http ://www.w3.org/2001/XMLSchema-instance");
nsMgr.AddNamesp ace("xsd","http ://www.w3.org/2001/XMLSchema");
nsMgr.AddNamesp ace("soap","htt p://schemas.xmlsoap .org/soap/envelope/");
Console.WriteLi ne("nsMgr.Defau ltNamespace: " + nsMgr.DefaultNa mespace);
XmlNode applicationNode = xmlRequest.Sele ctSingleNode("//application",
nsMgr);
if (applicationNod e == null)
{
Console.WriteLi ne("applicatio n not found");
}
else
{
Console.WriteLi ne(applicationN ode.InnerText.T rim());
}
Console.WriteLi ne(">>>Press a key to end");
Console.ReadLin e();
}
}
}
XML *****
<?xml version="1.0"?>
<AppRoot xmlns:xsd="http ://www.w3.org/2001/XMLSchema"
xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance">
<application xmlns="http://MyCompanyName.c o.uk/test/1.0.08/">
<docid>04231ESW M001240</docid>
<applicationerr or>false</applicationerro r>
<HoldIndicator> false</HoldIndicator>
<field>
<name>TextReg 1</name>
<text>STEP</text>
<error>false</error>
</field>
<field>
<name>portfolio _link_code</name>
<text>STEP</text>
<error>false</error>
</field>
</application>
</AppRoot>
Sorry if this is deemed to be a cross post (I also posted to the
microsoft.publi c.xml)
(Using .NET 2003, VB, C# on a Win2k PC)
I'm trying to retrieve nodes from an XML doc by name. I have found that the
following code, when translated into C# works perfectly, but not when used
in VB. In the example below the "applicatio n" node returns a value when
accessed through the C# code but not with the VB code.
If I add a prefix to the namespace in the source XML doc e.g. Change
<application xmlns="http://MyCompanyName.c o.uk/test/1.0.08/">
to <application xmlns:TEST="htt p://MyCompanyName.c o.uk/test/1.0.08/">
then it works fine in both languages.
Does anyone have experience of this problem ?
Below are the code samples from both languages... followed by the XML
excerpt
Thanks
Gary
VB Code --
****
Imports System
Imports System.Xml
Module Module1
Sub Main()
Try
Dim oXmlDoc As New Xml.XmlDocument
Dim sFileName As String = "04231ESWM00124 0.xml"
oXmlDoc.Load(sF ileName)
Dim nsmgr As XmlNamespaceMan ager = New
XmlNamespaceMan ager(oXmlDoc.Na meTable)
nsmgr.AddNamesp ace("clps",
"http://MyCompanyName.c o.uk/test/1.0.08")
nsmgr.AddNamesp ace("xsi",
"http://www.w3.org/2001/XMLSchema-instance")
nsmgr.AddNamesp ace("xsd", "http://www.w3.org/2001/XMLSchema")
nsmgr.AddNamesp ace("soap",
"http://schemas.xmlsoap .org/soap/envelope/")
Console.WriteLi ne("Def NS = " & nsmgr.DefaultNa mespace)
Dim oNode As Xml.XmlNode
oNode = oXmlDoc.SelectS ingleNode("//application", nsmgr)
If oNode Is Nothing Then
Console.WriteLi ne("The application object was not found")
Else
Console.WriteLi ne(oNode.InnerT ext)
End If
Finally
Console.WriteLi ne("Press a key to end")
Console.Read()
End Try
End Sub
and the C# version ****
using System;
using System.Xml;
namespace TestXML
{
class Class1
{
ry>
[STAThread]
static void Main(string[] args)
{
XmlDocument xmlRequest = new XmlDocument();
xmlRequest.Load (@"N:\04231ESWM 001240.xml");
XmlNamespaceMan ager nsMgr = new XmlNamespaceMan ager(xmlRequest .NameTable);
nsMgr.AddNamesp ace("clps","htt p://MyCompanyName.c o.uk/test/1.0.08/");
nsMgr.AddNamesp ace("xsi","http ://www.w3.org/2001/XMLSchema-instance");
nsMgr.AddNamesp ace("xsd","http ://www.w3.org/2001/XMLSchema");
nsMgr.AddNamesp ace("soap","htt p://schemas.xmlsoap .org/soap/envelope/");
Console.WriteLi ne("nsMgr.Defau ltNamespace: " + nsMgr.DefaultNa mespace);
XmlNode applicationNode = xmlRequest.Sele ctSingleNode("//application",
nsMgr);
if (applicationNod e == null)
{
Console.WriteLi ne("applicatio n not found");
}
else
{
Console.WriteLi ne(applicationN ode.InnerText.T rim());
}
Console.WriteLi ne(">>>Press a key to end");
Console.ReadLin e();
}
}
}
XML *****
<?xml version="1.0"?>
<AppRoot xmlns:xsd="http ://www.w3.org/2001/XMLSchema"
xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance">
<application xmlns="http://MyCompanyName.c o.uk/test/1.0.08/">
<docid>04231ESW M001240</docid>
<applicationerr or>false</applicationerro r>
<HoldIndicator> false</HoldIndicator>
<field>
<name>TextReg 1</name>
<text>STEP</text>
<error>false</error>
</field>
<field>
<name>portfolio _link_code</name>
<text>STEP</text>
<error>false</error>
</field>
</application>
</AppRoot>
Comment