How to parse an xsd file

I am writing a code generation tool that will take the XSD file created by the Visual Studio data generator and create my own class for each column in each table. I already understand how to implement IVsSingleFileGeneratorto generate code and how to turn this single-file generator into a multi-factor generator . However, it seems that I have the most difficult problem - this is the one that should be the simplest. I have never worked with XML or XML Schema before, and I don’t know what is the correct way to iterate through an XSD file and read column names and types so that I can create my code.

Any recommendation on a tutorial on reading an XSD file? Also any recommendations on how to pull out each xs:elementthat represents a column, and read its properties msprop:Generator_UserColumnName, typeand msprop:Generator_ColumnPropNameInTablefrom each element.

+3
source share
5 answers

According to btlog, XSD should be parsed as XML files. C # provides functionality for this.

XPath Tutorial: http://www.w3schools.com/xpath/default.asp
XQuery Tutorial: http://www.w3schools.com/xquery/default.asp
Random C # XmlDocumentTutorial: http://www.codeproject.com /KB/cpp/myXPath.aspx

# XPath/XQuery XmlDocument. , SelectSingleNode SelectNodes.

XmlDocument over XmlTextReader, - . , XmlTextReader.

. , Linq XML,.NET 4.0 XDocument XmlDocument. . XDocument XmlDocument.

+5

XmlSchemaSet, . , ,

XmlSchemaElement root = _schema.Items[0] as XmlSchemaElement;
XmlSchemaSequence children = ((XmlSchemaComplexType)root.ElementSchemaType).ContentTypeParticle as XmlSchemaSequence;
foreach(XmlSchemaObject child in children.Items.OfType<XmlSchemaElement>()) {
    XmlSchemaComplexType type = child.ElementSchemaType as XmlSchemaComplexType;
    if(type == null) {
        // It a simple type, no sub-elements.
    } else {
        if(type.Attributes.Count > 0) {
            // Handle declared attributes -- use type.AttributeUsers for inherited ones
        }
        XmlSchemaSequence grandchildren = type.ContentTypeParticle as XmlSchemaSequence;
        if(grandchildren != null) {
            foreach(XmlSchemaObject xso in grandchildren.Items) {
                if(xso.GetType().Equals(typeof(XmlSchemaElement))) {
                    // Do something with an element.
                } else if(xso.GetType().Equals(typeof(XmlSchemaSequence))) {
                    // Iterate across the sequence.
                } else if(xso.GetType().Equals(typeof(XmlSchemaAny))) {
                    // Good luck with this one!
                } else if(xso.GetType().Equals(typeof(XmlSchemaChoice))) {
                    foreach(XmlSchemaObject o in ((XmlSchemaChoice)xso).Items) {
                        // Rinse, repeat...
                    }
                }
            }
        }
    }
}

, , .

+9

XmlDocument. Xsd Xml, , , . XmlTextReader.

EDIT:

System.Xml.Schema.XmlSchema, , , , . http://msdn.microsoft.com/en-us/library/system.xml.schema.xmlschema.aspx .

+2

, tableadapters XSD. XML , - -. XSD, , . , .

Dim XMLDoc As New System.Xml.XmlDocument

XMLDoc.Load("MyDataset.xsd")
Dim oSortedTableAdapters As New Collections.Generic.SortedDictionary(Of String, Xml.XmlElement)

Const WebApplication As Boolean = False

Dim TableAdapters = XMLDoc.GetElementsByTagName("TableAdapter")
For Each TableAdapter As Xml.XmlElement In TableAdapters
    If WebApplication Then
        'pre-compiled way'
        oSortedTableAdapters.Add(TableAdapter.Attributes("GeneratorDataComponentClassName").Value, TableAdapter)
    Else
        'dynamic compiled way'
        oSortedTableAdapters.Add(TableAdapter.Attributes("Name").Value, TableAdapter)
    End If
Next
0
source

Just as you know, Visual Studio includes an XSD tool that already accepts an XSD file and generates classes for C # or VB.NET: http://msdn.microsoft.com/en-us/library/x6c1kb0s.aspx

0
source

Source: https://habr.com/ru/post/1755019/


All Articles