XML and XSD

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sharon

    #1

    XML and XSD

    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.SelectS ingleNode("desc endant::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"
    elementFormDefa ult="qualified"
    attributeFormDe fault="qualifie d" xmlns="http://tempuri.org/ClusterDS.xsd"
    xmlns:mstns="ht tp://tempuri.org/ClusterDS.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:msdata="u rn:schemas-microsoft-com:xml-msdata">
    <xs:element name="ClusterDS " msdata:IsDataSe t="true">
    <xs:complexType >
    <xs:choice maxOccurs="unbo unded">
    <xs:element name="Cluster">
    <xs:complexType >
    <xs:sequence>
    <xs:element name="ClusterID " msdata:ReadOnly ="true"
    msdata:AutoIncr ement="true" type="xs:long" />
    <xs:element name="FrameID" msdata:ReadOnly ="true"
    msdata:AutoIncr ement="true" type="xs:long" />
    <xs:element name="Type" type="xs:string " minOccurs="0" />
    <xs:element name="XCoordina te" type="xs:long" minOccurs="0" />
    <xs:element name="YCoordina te" type="xs:long" minOccurs="0" />
    <xs:element name="RedColor" type="xs:unsign edByte" minOccurs="0" />
    <xs:element name="GreenColo r" type="xs:unsign edByte" minOccurs="0" />
    <xs:element name="BlueColor " type="xs:unsign edByte" 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>20 0</XCoordinate>
    <YCoordinate>10 0</YCoordinate>
    <RedColor>0</RedColor>
    <GreenColor>0 </GreenColor>
    <BlueColor>25 5</BlueColor>
    </Cluster>
    <Cluster>
    <ClusterID>1</ClusterID>
    <FrameID>0</FrameID>
    <Type>0</Type>
    <XCoordinate>25 0</XCoordinate>
    <YCoordinate>30 0</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>


    ------------
    Thanks
    Sharon
  • Fabio Cannizzo

    #2
    Re: XML and XSD

    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


    "Sharon" <Sharon@discuss ions.microsoft. com> wrote in message
    news:46F389D8-A9E7-4121-A2CA-41E9476EF54B@mi crosoft.com...[color=blue]
    > 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.SelectS ingleNode("desc endant::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"
    > elementFormDefa ult="qualified"
    > attributeFormDe fault="qualifie d" xmlns="http://tempuri.org/ClusterDS.xsd"
    > xmlns:mstns="ht tp://tempuri.org/ClusterDS.xsd"
    > xmlns:xs="http://www.w3.org/2001/XMLSchema"
    > xmlns:msdata="u rn:schemas-microsoft-com:xml-msdata">
    > <xs:element name="ClusterDS " msdata:IsDataSe t="true">
    > <xs:complexType >
    > <xs:choice maxOccurs="unbo unded">
    > <xs:element name="Cluster">
    > <xs:complexType >
    > <xs:sequence>
    > <xs:element name="ClusterID " msdata:ReadOnly ="true"
    > msdata:AutoIncr ement="true" type="xs:long" />
    > <xs:element name="FrameID" msdata:ReadOnly ="true"
    > msdata:AutoIncr ement="true" type="xs:long" />
    > <xs:element name="Type" type="xs:string " minOccurs="0" />
    > <xs:element name="XCoordina te" type="xs:long" minOccurs="0" />
    > <xs:element name="YCoordina te" type="xs:long" minOccurs="0" />
    > <xs:element name="RedColor" type="xs:unsign edByte" minOccurs="0" />
    > <xs:element name="GreenColo r" type="xs:unsign edByte" minOccurs="0"
    > />
    > <xs:element name="BlueColor " type="xs:unsign edByte" 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>20 0</XCoordinate>
    > <YCoordinate>10 0</YCoordinate>
    > <RedColor>0</RedColor>
    > <GreenColor>0 </GreenColor>
    > <BlueColor>25 5</BlueColor>
    > </Cluster>
    > <Cluster>
    > <ClusterID>1</ClusterID>
    > <FrameID>0</FrameID>
    > <Type>0</Type>
    > <XCoordinate>25 0</XCoordinate>
    > <YCoordinate>30 0</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>
    >
    >
    > ------------
    > Thanks
    > Sharon[/color]


    Comment

    • Sharon

      #3
      Re: XML and XSD

      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);
      XmlNamespaceMan ager nsmgr = new XmlNamespaceMan ager(cluster.Na meTable);
      nsmgr.AddNamesp ace("ns", http://tempuri.org/ClusterDS.xsd);
      XmlNode node = cluster.SelectS ingleNode("desc endant::FrameID ", nsmgr);

      --------
      Thanks
      Sharon

      Comment

      • Sharon

        #4
        Re: XML and XSD

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

        XmlDocument cluster = new XmlDocument();
        cluster.LoadXml (xmlCluster);
        XmlNamespaceMan ager nsmgr = new XmlNamespaceMan ager(cluster.Na meTable);
        nsmgr.AddNamesp ace("ns", http://tempuri.org/ClusterDS.xsd);
        XmlNode node = cluster.SelectS ingleNode("desc endant::ns:Fram eID", 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?


        -------
        Thanks
        Sharon

        Comment

        Working...