XML Question using VB.Net Code
Hi All,
I was wondering if someone might be able to help me with a problem in VB.net code? I have the following XML file:
<Applications>
<ApplicationGroup GroupID="001" GroupName="Oracle SQLNet">
<Application AppName="SQLNet" Version="9.2.0.4">
<Package Name="Oracle SQLNet 9.2.0.4 DBA" id="">
<PackageInfo>
<MarkForUninstall>1</MarkForUninstall>
</PackageInfo>
</Package>
<Package Name="Oracle SQLNet 9.2.0.4 App User Std" id="">
<PackageInfo>
<MarkForUninstall>0</MarkForUninstall>
</PackageInfo>
</Package>
</Application>
</ApplicationGroup>
</Applications>
Im trying to do 2 things through use of the XMLDocument Class and XPath, get the Name attribute from each <Package> and value of <MarkForUninstall> for each of them. Any help would be great.
Thanks,
-ETR
Re: XML Question using VB.Net Code
This is C# Code but it should be easy for you to translate:
Code:
using System;
using System.Xml;
namespace XMLSample
{
class XMLSample
{
[STAThread]
static void Main(string[] args)
{
XmlDocument myDoc = new XmlDocument();
myDoc.LoadXml("<Applications><ApplicationGroup GroupID=\"001\" GroupName=\"Oracle SQLNet\"><Application AppName=\"SQLNet\" Version=\"9.2.0.4\"><Package Name=\"Oracle SQLNet 9.2.0.4 DBA\" id=\"\"><PackageInfo><MarkForUninstall>1</MarkForUninstall></PackageInfo></Package><Package Name=\"Oracle SQLNet 9.2.0.4 App User Std\" id=\"\"><PackageInfo><MarkForUninstall>0</MarkForUninstall></PackageInfo></Package></Application></ApplicationGroup></Applications>");
foreach(XmlNode myNode in myDoc.SelectNodes("//Package"))
{
string name = myNode.Attributes.GetNamedItem("Name").InnerText;
Console.WriteLine(name);
Console.WriteLine(myNode.SelectSingleNode("//Package[@Name='"+name+"']/PackageInfo/MarkForUninstall").InnerText);
}
Console.ReadLine();
}
}
}
This is just one possible way of doing it, there may be many more.
And of course you need to do some Exception handling in case your XPath Querys wont find a match! If you have any further questions just post!
Stephan