简体   繁体   English

使用XmlSchema获取对xml架构元素的引用

[英]Get a reference to the elements of a xml schema using XmlSchema

I want to use the class XmlSchema to parse a schema but I can't find how to get a reference to the Image and Size elements in the schema below. 我想使用XmlSchema类来解析架构,但是找不到以下架构中如何获取对Image和Size元素的引用。 I'm working on an app that will have a form based on a schema. 我正在开发一个具有基于模式的表单的应用程序。

I have the schema: 我有架构:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Test">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Image" type="FileUpload" />
        <xs:element name="Size" type="xs:int" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:simpleType name="FileUpload">
    <xs:restriction base="xs:string" />
  </xs:simpleType>
</xs:schema>

How can I do this? 我怎样才能做到这一点?

What about this..? 那这个呢..?

foreach(var size in doc.Root.DescendantNodes().OfType<XElement>()
        .Select(x => x.Size).Distinct())
{
    Console.WriteLine(size);
}

or 要么

foreach(var image in doc.Root.DescendantNodes().OfType<XElement>()
        .Select(x => x.Image).Distinct())
{
    Console.WriteLine(image);
}

you could also use XPATH 您也可以使用XPATH

XmlDocument xdoc = new XmlDocument(); 
xdoc.Load(path to your xml schema file);
XmlNodeList list = xdoc.SelectNodes("//Image");
XmlNodeList list2 = xdoc.SelectNodes("//Size");

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

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