Groups | Blog | Home
all groups > dotnet xml > may 2004 >

dotnet xml : XPath: Is there a better way to do this...



David Elliott
5/18/2004 3:44:13 PM
I am retrieving XmlSchema from a database and need to find the nodes that represent the Table and Column names.
I have included a sample Xml Schema and the code I used to find the information. As this is the first time I have
even attempted to use XPath, I was wondering if the code I provided is appropriate or if there is a better way
of doing this.

The <xs:element> tags are the nodes that I am looking for. As I am scraping a database, I am NOT guarenteed
to always have the exact structure below.

Any help is appreciated.
Dave


==========================================================
Output
==========================================================


Table
==============================
addr1
addr2
city
st
zip


Table1
==============================
name_first
name_last


Table2
==============================
book
isbn

==========================================================

<?xml version="1.0" standalone="yes" ?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Table">
<xs:complexType>
<xs:sequence>
<xs:element name="addr1" msdata:ReadOnly="true" type="xs:short" minOccurs="0" />
<xs:element name="addr2" msdata:ReadOnly="true" type="xs:string" minOccurs="0" />
<xs:element name="city" msdata:ReadOnly="true" type="xs:string" minOccurs="0" />
<xs:element name="st" msdata:ReadOnly="true" type="xs:string" minOccurs="0" />
<xs:element name="zip" msdata:ReadOnly="true" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Table1">
<xs:complexType>
<xs:sequence>
<xs:element name="name_first" type="xs:string" minOccurs="0" />
<xs:element name="name_last" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Table2">
<xs:complexType>
<xs:sequence>
<xs:element name="book" type="xs:string" minOccurs="0" />
<xs:element name="isbn" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>

==========================================================

using System;
using System.Xml;

class Class1
{
[STAThread]
static void Main(string[] args)
{
string file = args[0];

try
{
//Load the XML document
XmlDocument doc = new XmlDocument();
doc.Load(file);

//Load document's name table into XmlNamespaceManager
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("xs","http://www.w3.org/2001/XMLSchema");

XmlNode node = doc.SelectSingleNode("descendant::xs:element", ns);

if (node != null)
node = node.SelectSingleNode("descendant::xs:element", ns);

if (node != null)
{
do
{
Console.WriteLine("{0}", node.Attributes.Item(0).InnerText);
Console.WriteLine("==============================");

XmlNodeList rNodes = node.SelectNodes("descendant::xs:element", ns);
foreach (XmlNode node1 in rNodes)
{
Console.WriteLine("{0}", node1.Attributes.Item(0).InnerText);
}

Console.WriteLine("\n");
node = node.NextSibling;
}
while (node != null);
}

}
catch(Exception exp)
{
Console.WriteLine("Error: " + exp.ToString());
}

Console.Write("Press Enter to continue...");
Console.ReadLine();

}
}

Yan Leshinsky
5/20/2004 4:16:19 PM
Your code looks OK. What are your concerns?
Yan

[quoted text, click to view]

AddThis Social Bookmark Button