tags:

views:

1046

answers:

2

Hello

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"&gt;
  <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.

+2  A: 

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
    }
}
Dexter
Hello. I am programming a xsd file parser script. there is some foreach (find element) lines in my scipt. But i can't read the Value. That's why bothed me a lot. If i referenced as an element. acturally there is several elements in my XSD file.
Nano HE
@Dexter. from your script above, the xe.Name return "xsd:schema". Suppose the XSD is a XML format file too. The return value is the root element. No doubt. // But i still can't get the root element value :"RootValueHere" Is there any RootElemnt Property for Schema file?
Nano HE
@Nano - apologies - I've now corrected the code snippet and expanded it a bit further
Dexter
@Dexter, THANKS a lot!
Nano HE
+1  A: 

I believe

XmlDocument myDocument = new XmlDocument("my.xml");
myDocument.DocumentElement(); //gets root document node
Woot4Moo
I work together with my team for my project. I handle job of the XSD parse. I must find out the (only one in my XSD file) root element from the XSD file. then generate a list for my team member who could validate the XML file based on my result. (She can't find the root element from my list, and she already could get the XML file with the method you provide above.
Nano HE
since an XSD is a XmlDocument I don't see how that would fail. However, the only other thing I can think is to use an xpath query
Woot4Moo
We're talking about two different things here - one is parsing the XSD document, and the other is parsing the XML documents which describe data in the format defined by the XSD - @Nano is talking about parsing the XSD, but validating the XML document
Dexter