简体   繁体   English

我可以在加载xml文件时指定架构来构建XDocument吗?

[英]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. 我是xml新手,试图从xml文件创建XDocument类型。

I can validate the xml against a schema. 我可以针对架构验证xml。

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. 但是XDocument的结构似乎是错误的。

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. 根变量包含完整的xml字符串,其他IEnumerable变量保留为空。 What am i missing? 我想念什么?

EDIT: This is part of the xml and the xsd 编辑:这是xml和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的一部分

<?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 . 我没有看到任何其他方式,使用出色的工具Xsd2Code从xsd创建了类。 Using a serializer i am able to get all data from the xml into an object graph. 使用序列化程序,我能够将xml中的所有数据转换为对象图。 This solved my problem even if it did not answer my question. 即使没有回答我的问题,这也解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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