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

发布于 2024-12-24 23:20:17 字数 3317 浏览 0 评论 0原文

我是一名 xml 新手,试图从 xml 文件创建 XDocument 类型。

我可以根据架构验证 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);
    }
}

但XDocument的结构似乎是错误的。

运行此代码时,

    [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);
    }

根变量包含完整的 xml 字符串,其他 IEnumerable 变量保持为空。我缺少什么?

编辑: 这是 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>

这是 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太阳哥哥 2024-12-31 23:20:17

在没有看到任何其他方式的情况下,我使用出色的工具 Xsd2Code 从 xsd 创建类。使用序列化器,我可以将 xml 中的所有数据获取到对象图中。这解决了我的问题,即使它没有回答我的问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文