XML and XSD

G

Guest

I'm using an XML document that has a list of 'Clusters', this XML file is
made using an XML schema (XSD),
When I;m using the following code:
XmlDocument cluster = new XmlDocument();
cluster.LoadXml(xmlCluster);
XmlNode node = cluster.SelectSingleNode("descendant::FrameID"); // Errror:
Return null;

The node is null.
But when I remove the schema from the XML file is works fine.

Can anybody explain why it does not work when the schema is attached to the
XML file?
What code will work in both cases?

The XSD file:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="ClusterDS" targetNamespace="http://tempuri.org/ClusterDS.xsd"
elementFormDefault="qualified"
attributeFormDefault="qualified" xmlns="http://tempuri.org/ClusterDS.xsd"
xmlns:mstns="http://tempuri.org/ClusterDS.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="ClusterDS" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Cluster">
<xs:complexType>
<xs:sequence>
<xs:element name="ClusterID" msdata:ReadOnly="true"
msdata:AutoIncrement="true" type="xs:long" />
<xs:element name="FrameID" msdata:ReadOnly="true"
msdata:AutoIncrement="true" type="xs:long" />
<xs:element name="Type" type="xs:string" minOccurs="0" />
<xs:element name="XCoordinate" type="xs:long" minOccurs="0" />
<xs:element name="YCoordinate" type="xs:long" minOccurs="0" />
<xs:element name="RedColor" type="xs:unsignedByte" minOccurs="0" />
<xs:element name="GreenColor" type="xs:unsignedByte" minOccurs="0" />
<xs:element name="BlueColor" type="xs:unsignedByte" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>

The XML file:

<?xml version="1.0" encoding="utf-8"?>
<ClusterDS xmlns="http://tempuri.org/ClusterDS.xsd">
<Cluster>
<ClusterID>0</ClusterID>
<FrameID>0</FrameID>
<Type>0</Type>
<XCoordinate>200</XCoordinate>
<YCoordinate>100</YCoordinate>
<RedColor>0</RedColor>
<GreenColor>0</GreenColor>
<BlueColor>255</BlueColor>
</Cluster>
<Cluster>
<ClusterID>1</ClusterID>
<FrameID>0</FrameID>
<Type>0</Type>
<XCoordinate>250</XCoordinate>
<YCoordinate>300</YCoordinate>
<RedColor>0</RedColor>
<GreenColor>255</GreenColor>
<BlueColor>0</BlueColor>
</Cluster>
<Cluster>
<ClusterID>2</ClusterID>
<FrameID>0</FrameID>
<Type>0</Type>
<XCoordinate>10</XCoordinate>
<YCoordinate>50</YCoordinate>
<RedColor>255</RedColor>
<GreenColor>0</GreenColor>
<BlueColor>0</BlueColor>
</Cluster>
</ClusterDS>
 
F

Fabio Cannizzo

I suggest you try the following, which is normally an easy way to deal with
xml.

1) Get ALTOVA home edition.
2) Check the XML file is consistent with the XSD file (well built) in
ALTOVA.
3) Generate a dataset class from the XSD file using VS utility xsd.exe
4) Use the genarated dataset class to read your xml into dataset tables.
5) Do whatever you want with your data, which is now nicely imported into
tables.

Fabio
 
G

Guest

I this it's too much and I do not need the DataSet at this point.

There is a simpler solution:
Adding a namespace manager to your query, something like:

XmlDocument cluster = new XmlDocument();
cluster.LoadXml(xmlCluster);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(cluster.NameTable);
nsmgr.AddNamespace("ns", http://tempuri.org/ClusterDS.xsd);
XmlNode node = cluster.SelectSingleNode("descendant::FrameID", nsmgr);
 
G

Guest

Sorry, I had a mistake. The code one is:

XmlDocument cluster = new XmlDocument();
cluster.LoadXml(xmlCluster);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(cluster.NameTable);
nsmgr.AddNamespace("ns", http://tempuri.org/ClusterDS.xsd);
XmlNode node = cluster.SelectSingleNode("descendant::ns:FrameID", nsmgr);


But still the code is different in case the XML file does not attached to a
schema.

Isn't is a way to have the same code in both cases?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top