Does anyone have an idea about how keep track of a paths in xsd in deep-first traverse
For example: if I have this schema,
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:books" xmlns:bks="urn:books">
<xsd:element name="books" type="bks:BooksForm"/>
<xsd:complexType name="BooksForm">
<xsd:sequence>
<xsd:element name="book" type="bks:BookForm" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="BookForm">
<xsd:sequence>
<xsd:element name="author" type="xsd:string"/>
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="genre" type="xsd:string"/>
<xsd:element name="price" type="xsd:float" />
<xsd:element name="pub_date" type="xsd:date" />
<xsd:element name="review" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string"/>
</xsd:complexType>
</xsd:schema>
how can I get like this output in c#
path1 : books.book.author
path2 : books.book.title
.. so on
for any schema structure
does anyone have an idea about this or any good starting points
thanks in advance
thanks for reply
I tried to code your advice but I am still getting stuck
while (r.Read())
{
switch (r.Name)
{
case "xsd:element":
myStringBuilder.Append(r.GetAttribute("name"));
break;
case "xsd:complexType":
checkComplex(r);
wholepath += r.GetAttribute("name");
//this will only concatenate complex elements only
Console.WriteLine("checkComplexcaller{1}", wholepath);
break;
}
}
for recursive part.. I did
public static void checkComplex(/*what I should send here*/)
{
if (r.GetAttribute("name") == "xsd:complexType")
{
//What I sould to do for this recursive
}
else if (r.GetAttribute("name") == "xsd:element")
{
myStringBuilder.Append(r.GetAttribute("name"));
}
}
How could setup the path correctly
If I am getting this right: (pseudocode)
(make a xml document of the xsd)
foreach node of type xsd:element:
track path to the node as input
apend it with values of 'name' and 'type' attributes
if its a complex type: apply this recursively on all elements
Related
This is probably very simple for you guys, but I need to be able to count the attribute names within an xml document when validating xml against a schema using c#. Specifically duplicate attribute names (not valid).
If my xsd has these in it:
<xsd:schema>
<xsd:complexType name="scheduleEvent">
<xsd:all>
<xsd:element name="Basic" type="MyBasic"/>
</xsd:all>
</xsd:complexType>
<xsd:complexType name="MyBasic">
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element name="Descriptor" type="Descriptor" maxOccurs="1"/>
<xsd:element name="Descriptor1" type="Descriptor1" maxOccurs="1"/>
</xsd:choice>
</xsd:complexType>
<xsd:complexType name="Descriptor">
<xsd:attribute name="Test" type="typ:Test"/>
</xsd:complexType>
<xsd:complexType name="Descriptor1">
<xsd:attribute name="Test1" type="typ:Test1"/>
</xsd:complexType>
And my xml to validate against looks like (not valid, I know just a quick mock up example for reference:
Declarations etc...
<ScheduleEvent>
<MyBasic>
<Descriptor Test="02"/>
<Descriptor1 Test1="02" Test1="02"/>
</MyBasic>
</ScheduleEvent>
How can I count the number of "Test1" attributes? C# xmlreader (without using exceptions, long story).
First of all, that isn't a valid xml. An attribute is unique per element.
If what you ment is that you can have multiple "Test1" attributes throughout the different elements, then you can do the following using LINQ to XML:
var xml = XDocument.Load(#"PathToXml");
var testCount = xml.Descendants().Attributes("Test1").Count();
Try this :
XDocument doc=XDocument.Load(XMLFile or XMLFilePath);
var Sample=doc.Descendants("Test1").Count();
I need to call a Web Service, created by someone else, from a .Net C# application.
The web service requires me to send a list of items ("articles"), without enclosing them in a parent element. This is a simplified illustration of the "desired" look of the call:
<body>
<info>something</info>
<moreinfo>something else</moreinfo>
<article>
<articleno>123</articleno>
</article>
<article>
<articleno>456</articleno>
</article>
</body>
On my end, the articles is a generic List<Article>, and my call to the web service tends to contain something looking more like this:
<body>
<info>something</info>
<moreinfo>something else</moreinfo>
<articles><!-- parent element -->
<article>
<articleno>123</articleno>
</article>
<article>
<articleno>456</articleno>
</article>
</articles><!-- parent element end -->
</body>
The WSDL file from the web service is corrupted, so I've hade to manually create a working subset of the functions in it that I need. Using that "hand coded" WSDL I add a Service Reference using Visual Studio 2013.
I have been trying to solve the problem by modifying the WSDL, and by modifying the code that is created by Visual Studio when adding the service reference, but so far I haven't succeeded.
I've bee trying various attributes for the list parameter, such as XmlElement, XmlIgnore, MessageHeaderArrayAttribute and others, but to be honest I don't know how they work or which ones could actually be useful in this scenario.
Can the WSDL be altered to make Visual Studio's auto generated code create the correct output when calling the web service? (Preferred solution)
Or is there a way to force soap serialization to produce the list of articles without an enclosing element? (Acceptable solution)
Here is a simplified version of the relevant (as far as I can tell) portions of the WSDL
<xsd:element name="PriceRequest">
<xsd:complexType>
<xsd:sequence>
<xsd:element minOccurs="0" maxOccurs="1" name="info" type="xsd:string" />
<xsd:element minOccurs="0" maxOccurs="1" name="moreinfo" type="xsd:string" />
<xsd:element minOccurs="0" maxOccurs="1" name="articles" type="tns:ArrayOfArticlestructObj" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
...
<xsd:complexType name="ArticlestructObj">
<xsd:sequence>
<xsd:element minOccurs="0" maxOccurs="1" name="articleno" type="xsd:string" />
<xsd:element minOccurs="0" maxOccurs="1" name="otherinfo..." type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
<xsd:element name="ArticlestructObj" type="tns:ArticlestructObj" />
<xsd:complexType name="ArrayOfArticlestructObj">
<xsd:sequence>
<xsd:element minOccurs="0" maxOccurs="unbounded" name="ArticlestructObj" nillable="true" type="tns:Articlestruct" />
</xsd:sequence>
</xsd:complexType>
<xsd:element name="ArrayOfArticlestruct" type="tns:ArrayOfArticlestruct" />
I am a noob on WSDL as well as on StackOverflow, so please excuse me if this question is hard to read.
I am using Visual studio 2008 C# Windows application to connect to a webservice created by an external company. The service is a WSDL url, which is a basic soap request and response. I have inported the service as a service reference. I can call the service, and receive no errors, but the reponse is nothing. When monitoring the soap request and response in Fiddler, i see a response coming back, but it's as if VS cannot interpret the response. I have looked at creating xsd's, but get an error stating that there's already a schema. So I am at a total loss. I hope someone can help. I apologise if this exact issue has been solved before, but i cannot find anything that resolves my problem.
Thanks in advance!
The code I am using to connect to the service is as follows:
ServiceReference1.bpm bbb = new TestingWSDL.ServiceReference1.bpm();
ServiceReference1.BPMExternalAppServicesV001Client soapClient = new TestingWSDL.ServiceReference1.BPMExternalAppServicesV001Client();
ServiceReference1.DownloadShipmentDataV001 req = new TestingWSDL.ServiceReference1.DownloadShipmentDataV001();
ServiceReference1.DownloadShipmentDataV001Response resp = new TestingWSDL.ServiceReference1.DownloadShipmentDataV001Response();
try
{
soapClient.DownloadShipmentDataV001(req,out resp);
}
catch(Exception err)
{
MessageBox.Show(err.Message);
}
the response object does not have any data in. I cannot post an image, as i am not allowed.
The WSDL is quite long and is as follows:
<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:tns="http://schemas.ccc.com/AROPInventory/bpm/external" xmlns:inst="http://schemas.aaa.com/bpm/instance/1.0" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" name="BPMExternalAppServicesV001" targetNamespace="http://schemas.ccc.com/AROPInventory/bpm/external" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<xsd:schema xmlns:tns1="http://schemas.ccc.com/AROPInventory/bpm/external" xmlns="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://schemas.argility.com/AROPInventory/bpm/external" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:import namespace="http://schemas.aaa.com/bpm/instance/1.0" />
<xsd:element name="UploadShipmentDataV001">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="tns1:Shipments" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/" name="Shipments">
<xsd:complexType>
<xsd:sequence>
<xsd:element minOccurs="0" maxOccurs="unbounded" name="Shipment">
<xsd:complexType>
<xsd:sequence>
<xs:element name="ShipmentNumber" type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" />
<xsd:element name="Cartons">
<xsd:complexType>
<xsd:sequence>
<xsd:element minOccurs="0" maxOccurs="unbounded" name="Carton">
<xsd:annotation>
<xsd:documentation>0:not damaged 1:damaged 2: Rejected0:not damaged 1:damaged 2: Rejected0:not damaged 1:damaged 2: Rejected</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:sequence>
<xs:element name="CartionID" type="xs:byte" xmlns:xs="http://www.w3.org/2001/XMLSchema" />
<xs:element name="Status" type="xs:byte" xmlns:xs="http://www.w3.org/2001/XMLSchema" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="TestUploadShipMentV001">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="tns1:Shipments" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="TestUploadShipMentV001Response" />
<xsd:element name="UploadShipmentDataV001Response" />
<xsd:element name="DownloadShipmentDataV001">
<xsd:complexType>
<xsd:sequence />
</xsd:complexType>
</xsd:element>
<xsd:element name="DownloadShipmentDataV001Response">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="tns1:Shipments" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://schemas.aaa.com/bpm/instance/1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="bpm">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="instance_id" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
<wsdl:message name="DownloadShipmentDataV001Input">
<wsdl:part name="body" element="tns:DownloadShipmentDataV001" />
</wsdl:message>
<wsdl:message name="DownloadShipmentDataV001Output">
<wsdl:part name="body" element="tns:DownloadShipmentDataV001Response" />
</wsdl:message>
<wsdl:message name="HeaderOutput">
<wsdl:part name="BPMHeader" element="inst:bpm" />
</wsdl:message>
<wsdl:portType name="BPMExternalAppServicesV001">
<wsdl:operation name="DownloadShipmentDataV001">
<wsdl:input message="tns:DownloadShipmentDataV001Input" />
<wsdl:output message="tns:DownloadShipmentDataV001Output" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="BPMExternalAppServicesV001" type="tns:BPMExternalAppServicesV001">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="DownloadShipmentDataV001">
<soap:operation soapAction="" style="document" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
<soap:header message="tns:HeaderOutput" part="BPMHeader" use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="BPMExternalAppServicesV001Service">
<wsdl:port name="BPMExternalAppServicesV001Port" binding="tns:BPMExternalAppServicesV001">
<soap:address location="http://aaa/aaa/com.eibus.web.soap.Gateway.wcp?organization=o=system,cn=aaa,cn=aaa,o=ho.bbb.co.za" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
The fiddler response is as follows:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<header xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
xmlns="http://schemas.aaa.com/General/1.0/">
<sender>
<reply-to>
cn=Business Process Management,cn=Business Process Management,cn=soap nodes,o=system,cn=aaa,cn=aaa20,o=ho.bbb.co.za
</reply-to>
<organizationalContext>o=system,cn=aaa,cn=aaa20,o=ho.bbb.co.za</organizationalContext>
<component>cn=Business Process Management,cn=soap nodes,o=system,cn=aaa,cn=aaa20,o=ho.bbb.co.za</component>
</sender>
<receiver>
<component>cn=webgateway#poc-aaa,cn=aaa,cn=aaa20,o=ho.bbb.co.za</component>
<sent-to xmlns="http://schemas.aaa.com/General/1.0/">socket://poc-aaa:20379/</sent-to>
</receiver>
<msg-id>00505680-004E-11E1-FBF1-7CB604BB1FC0</msg-id>
</header>
<bpm xmlns="http://schemas.aaa.com/bpm/instance/1.0">
<instance_id>26c34038-bea4-459c-a0ad-86b462ae90cb</instance_id>
</bpm>
</s:Header>
<s:Body>
<DownloadShipmentDataV001Response xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.argility.com/AROPInventory/bpm/external">
<Shipments xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.argility.com/AROPInventory/bpm/external">
<ShipmentHeader>
<ShipmentNumber>S001</ShipmentNumber>
<ReceivingBranch>B000028</ReceivingBranch>
<SendingBranch>B000001</SendingBranch>
<ExpectedDate>2012/07/30</ExpectedDate>
<TotalNrOfCartons>2</TotalNrOfCartons>
<ShipmentStatus>E</ShipmentStatus>
<Cartons>
<Carton>
<CartonHeader>
<NrOfItems>2</NrOfItems>
<CartionID>99</CartionID>
</CartonHeader>
<CartonDetail>
<SKU>12345</SKU>
<Description>abc</Description>
<SerialNumber>6789</SerialNumber>
<QtySent>20</QtySent>
<QtyReceived/>
</CartonDetail>
<CartonDetail>
<SKU>12390</SKU>
<Description>abc</Description>
<SerialNumber>67232</SerialNumber>
<QtySent>2</QtySent>
<QtyReceived/>
</CartonDetail>
</Carton>
<Carton>
<CartonHeader>
<NrOfItems>2</NrOfItems>
<CartionID>100</CartionID>
</CartonHeader>
<CartonDetail>
<SKU>12345</SKU>
<Description>abc</Description>
<SerialNumber>6789</SerialNumber>
<QtySent>20</QtySent>
<QtyReceived/>
</CartonDetail>
<CartonDetail>
<SKU>12390</SKU>
<Description>abc</Description>
<SerialNumber>67232</SerialNumber>
<QtySent>2</QtySent>
<QtyReceived/>
</CartonDetail>
</Carton>
</Cartons>
</ShipmentHeader>
<ShipmentHeader>
<ShipmentNumber>S002</ShipmentNumber>
<ReceivingBranch>B000029</ReceivingBranch>
<SendingBranch>B000002</SendingBranch>
<ExpectedDate>2012/06/3</ExpectedDate>
<TotalNrOfCartons>21</TotalNrOfCartons>
<ShipmentStatus>E</ShipmentStatus>
<Cartons>
<Carton>
<CartonHeader>
<NrOfItems>3</NrOfItems>
<CartionID>91</CartionID>
</CartonHeader>
<CartonDetail>
<SKU>12345</SKU>
<Description>abc</Description>
<SerialNumber>6789</SerialNumber>
<QtySent>20</QtySent>
<QtyReceived/>
</CartonDetail>
<CartonDetail>
<SKU>12390</SKU>
<Description>abc</Description>
<SerialNumber>67232</SerialNumber>
<QtySent>2</QtySent>
<QtyReceived/>
</CartonDetail>
</Carton>
<Carton>
<CartonHeader>
<NrOfItems>1</NrOfItems>
<CartionID>97</CartionID>
</CartonHeader>
<CartonDetail>
<SKU>12390</SKU>
<Description>abc</Description>
<SerialNumber>67232</SerialNumber>
<QtySent>2</QtySent>
<QtyReceived/>
</CartonDetail>
</Carton>
</Cartons>
</ShipmentHeader>
</Shipments>
</DownloadShipmentDataV001Response>
</s:Body>
</s:Envelope>
Thanks for posting the response as collected by Fiddler. I am actually seeing more than one problem in the WSDL and in the response conformance to the WSDL datatypes.
First issue: WSDL consistency/completeness
Note that the input and output messages refer to tns:DownloadShipmentDataV001 and tns:DownloadShipmentDataV001Response, respectively. xmlns:tns="http://schemas.ccc.com/AROPInventory/bpm/external" is the relevant prefix:namespace binding in the context of the message definitions, thus the fully-qualified names of the request and response message elements are {http://schemas.ccc.com/AROPInventory/bpm/external}DownloadShipmentDataV001 and {http://schemas.ccc.com/AROPInventory/bpm/external}DownloadShipmentDataV001Response, respectively.
By virtue of the targetNamespace="http://schemas.argility.com/AROPInventory/bpm/external" declaration, the fully-qualified name of the elements defined in the types section of the WSDL document are
{http://schemas.argility.com/AROPInventory/bpm/external}DownloadShipmentDataV001 and {http://schemas.argility.com/AROPInventory/bpm/external}DownloadShipmentDataV001Response. This is a problem because the actual types referred to in the message definitions are missing from the WSDL and any imported/included documents.
A stub generator should complain about this when trying to consume the WSDL.
The external company should really correct this issue, but you could copy the WSDL and make a tweak by declaring the namespace of the actual element from the types section and binding to a prefix, then using that prefix in the message definition (weakness being you have an extra step in keeping up with any changes in their service definition):
xmlns:fromtype="http://schemas.argility.com/AROPInventory/bpm/external"
...
<wsdl:message name="DownloadShipmentDataV001Input">
<wsdl:part name="body" element="fromtype:DownloadShipmentDataV001" />
</wsdl:message>
<wsdl:message name="DownloadShipmentDataV001Output">
<wsdl:part name="body" element="fromtype:DownloadShipmentDataV001Response" />
</wsdl:message>
Second issue: Response Message Does Not Match Schema
Assume that the WSDL was consistent and called out the fully-qualified datatype from the types section. The response message's fully-qualified element structure would then be expected to be something like:
{http://schemas.argility.com/AROPInventory/bpm/external}DownloadShipmentDataV001Response
{http://schemas.argility.com/AROPInventory/bpm/external}Shipments
{http://schemas.argility.com/AROPInventory/bpm/external}Shipment
{http://schemas.argility.com/AROPInventory/bpm/external}ShipmentNumber
{http://schemas.argility.com/AROPInventory/bpm/external}Cartons
{http://schemas.argility.com/AROPInventory/bpm/external}Carton
...
However, the actual structure in the response message is:
{http://schemas.argility.com/AROPInventory/bpm/external}DownloadShipmentDataV001Response
{http://schemas.argility.com/AROPInventory/bpm/external}Shipments
{http://schemas.argility.com/AROPInventory/bpm/external}ShipmentHeader
{http://schemas.argility.com/AROPInventory/bpm/external}ShipmentNumber
...
Note that ShipmentHeader is never declared as an element in the WSDL types section and is not imported from another document. If the unmarshaller is using the datatype definition provided in the WSDL, it does not understand the ShipmentHeader or its subelements so probably would leave those out (as you are seeing).
The correction for this issue is to match up the datatype schema definition with the actual response from the service. Again, the external organization hosting the service should correct this (their service is not in compliance with the given WSDL), but you could again tweak locally with the same caveat about extra steps keeping 'your' wsdl in lock-step with their service.
I hope this helps and respond back with further questions.
You will still need to parse this XML file yourself.
We have a similar problem with IBM's Remedy software. Basically it is terrible software and this behavior should be expected. Even if they update the code to match they are not generating the WSDL from the actual XML document they are generating... this problem will continue until they change their methods and you have no control over that.
I had a similar problem with empty answer but in fiddler it said the opposite. I fixed it by removing the
`System.ServiceModel.XmlSerializerFormatAttribute attribute and was able to serialize the response. Not the optimal solution but it worked for me.
I have the following in a WSDL I am consuming;
<xsd:complexType name="SomeClassType">
<xsd:sequence>
<xsd:element type="xsd:string" name="errorMessage" minOccurs="1" nillable="true" maxOccurs="1"> </xsd:element>
<xsd:element type="tp:ArrayOfArrayOfString" name="values" minOccurs="1" nillable="true" maxOccurs="1"> </xsd:element>
<xsd:element type="xsd:boolean" name="isEmpty" minOccurs="1" maxOccurs="1"> </xsd:element>
</xsd:sequence>
</xsd:complexType>
where
<xsd:complexType name="ArrayOfArrayOfString">
<xsd:complexContent>
<xsd:restriction base="soapenc:Array">
<xsd:attribute ref="soapenc:arrayType" wsdl:arrayType="xsd:string[,]"></xsd:attribute>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
However using wsdl.exe from MS (Runtime Version: 1.1.4322.573) generates
public class SomeClassType {
///
public string errorMessage;
///
public string[] values;
///
public bool isEmpty;
}
I expected string[,] values not string[] values
Is there a fix or a work around to this problem? (other than manually changing the generated code)
I had to set the type="tp:ArrayOfArrayOfString" to type="tp:ArrayOfString" and the maxOccurs="unbounded"
I believe you could try the WCF proxy generator (I believe WCF uses some other util, not wsdl.exe) - maybe that would be useful, but if that fails - I think that manually editing the generated code is your only option.
Try svcutil.exe. it is advisable to try a newer version of .net.
Good day.
As i know. There is a root element in XML file.
But from the XSD file structure, it is not easy to get the root element value.
Is there any method to do it?
(I wouldn't like to take use of hard code to find XSD root element value in my project.
i want to find the root element of "RootValueHere"
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="RootValueHere">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="DocumentInfo" minOccurs="1" maxOccurs="1" />
<xsd:element ref="Prerequisite" minOccurs="1" maxOccurs="1" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<!-- Element of DocumentInfo -->
<xsd:element name="DocumentInfo">
<xsd:complexType>
<xsd:attribute name="Name" type="xsd:string" />
<xsd:attribute name="Description" type="xsd:string" />
<xsd:attribute name="Creator" type="xsd:string" />
<xsd:attribute name="CreateTime" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<!-- Element of Prerequisite -->
<xsd:element name="Prerequisite">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Type" type="Prerequisite.Type.type" minOccurs="1" maxOccurs="1" />
<xsd:element name="Miscellaneous" type="Prerequisite.Misc.type" minOccurs="0" maxOccurs="1" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
thank you.
Whilst a single document can only contain one root element, as XSD can actually define multiple valid root elements.
If you only genuinely want a single type to be valid as a root element, then it should be the only type referenced as an <element>.
In your schema above, for example, the DocumentInfo and Prerequisite nodes are valid root elements too. To restrict your schema to have just a single, valid root node, replace your DocumentInfo and Prerequisite elements with simple complexType definitions:
<xsd:complexType name="DocumentInfoType">
...
</xsd:complexType>
<xsd:complexType name="Prerequisite">
....
</xsd:complexType>
UPDATE: To access the name of an element, you just need to look at the Name property on the XmlElement:
XmlDocument doc = new XmlDocument();
doc.Load("D:\\schema.xsd"); // Load the document from the root of an ASP.Net website
XmlElement schemaElement = doc.DocumentElement; // The root element of the schema document is the <schema> element
string elementName = schemaElement.LocalName; // This will print "schema"
foreach (XmlNode ele in schemaElement.ChildNodes)
{
if (ele.LocalName == "element")
{
// This is a valid root node
// Note that there will be *more than one* of these if you have multiple elements declare at the base level
}
}
I believe
XmlDocument myDocument = new XmlDocument("my.xml");
myDocument.DocumentElement(); //gets root document node