简体   繁体   中英

Can i specify a schema to build an XDocument while loading a xml file?

I am a xml newbie trying to create a XDocument type from a xml file.

I can validate the xml against a schema.

public class XmlHandler
{
    public XDocument Read(string filename, string schemaname)
    {
        var schemas = this.GetSchemas(schemaname);
        var doc = XDocument.Load(filename);

        var invalid = false;
        doc.Validate(schemas,
            (o, args) =>
                {
                    this.OnValidationErrors(o, args);
                    invalid = true;
                });

        return invalid ? new XDocument() : doc;
    }

    public XmlSchemaSet GetSchemas(string schemaname)
    {
        var schemas = new XmlSchemaSet();
        schemas.Add(null, schemaname);
        return schemas;
    }

    private void OnValidationErrors(object sender, ValidationEventArgs e)
    {
        Debug.Print("Errors: ", e);
    }
}

But the structure of the the XDocument seems to be wrong.

When running this code

    [Fact]
    public void Read_get_elements()
    {
        var sut = new XmlHandler();

        var result = sut.Read(this.TestFile, this.TestFileSchema);

        var root = result.Root;
        var elements = result.Elements();
        var nodes = result.Nodes();
        var descendants = result.Descendants();

        Assert.NotEmpty(elements);
    }

the root variable contains the complete xml string and the the other IEnumerable variables stay empty. What am i missing?

EDIT: This is part of the xml and the xsd

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
       targetNamespace="https://www.eurexchange.com/members/releases/eurex14/manuals_technical_en.html" 
       xmlns="https://www.eurexchange.com/members/releases/eurex14/manuals_technical_en.html" 
       elementFormDefault="qualified">

  <xs:include schemaLocation="eurex_reports_common_structs.xsd"/>

  <xs:complexType name="cb020Type">
    <xs:annotation>
      <xs:documentation>CB020 Position Summary</xs:documentation>
    </xs:annotation>
    <xs:sequence>
      <xs:element name="rptHdr" type="rptHdrType" />
      <xs:element name="cb020Grp" type="cb020GrpType" minOccurs="0" maxOccurs="unbounded" />
    </xs:sequence>
  </xs:complexType>

  <xs:element name="cb020" type="cb020Type"/>

  <xs:complexType name="cb020GrpType">
    <xs:sequence>
      <xs:element name="cb020KeyGrp" type="cb020KeyGrpType" />
      <xs:element name="cb020Grp1" type="cb020Grp1Type" minOccurs="1" maxOccurs="unbounded" />
    </xs:sequence>
  </xs:complexType>
</xs:schema>

And this is part of the xml

<?xml version="1.0" encoding="UTF-8"?>
<cb020 xmlns="https://www.eurexchange.com/members/releases/eurex14/manuals_technical_en.html">
  <rptHdr>
    <exchNam>EUREX</exchNam>
    <envText>P</envText>
    <rptCod>CB020</rptCod>
    <rptNam>Position Summary</rptNam>
    <membLglNam>Cyberdyne Systems</membLglNam>
    <rptPrntEffDat>2011-12-05</rptPrntEffDat>
    <rptPrntRunDat>2011-12-05</rptPrntRunDat>
  </rptHdr>
  <cb020Grp>
    <cb020KeyGrp>
      ...
    </cb020KeyGrp>
    <cb020Grp1>
      ...
    </cb020Grp1>
  </cb020Grp>
</cb020>

Without seeing any other way i created classes from the xsd with the wonderful tool Xsd2Code . Using a serializer i am able to get all data from the xml into an object graph. This solved my problem even if it did not answer my question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM