从.NET XSD架构文件解压缩枚举值解压缩、架构、文件、NET

2023-09-03 03:59:20 作者:安穩

从XSD架构文件使用.NET编程如何提取元素的枚举约束值?

例如,我想提取从以下XSD奥迪,高尔夫和宝马:

 < XS:元素的名称=车>
  < XS:简单类型>
    < XS:限制基地=XS:字符串>
      < XS:枚举值=奥迪/>
      < XS:枚举值=高尔夫/>
      < XS:枚举值=宝马/>
    < / XS:限制>
  < / XS:简单类型>
< / XS:组件>
 

解决方案

有一个的 的XmlSchema 类,但它看起来pretty的......好玩的工作。

请问XML查询就够了吗?

  XmlDocument的文档=新的XmlDocument();
doc.Load(Foo.xsd);
XmlNamespaceManager的经理=新的XmlNamespaceManager(doc.NameTable);
mgr.AddNamespace(XX,http://www.w3.org/2001/XMLSchema);
的foreach(的XmlElement EL在doc.SelectNodes(// XX:元素[@名称='车'] [1] / XX:简单类型/ XX:限制/ XX:枚举,经理))
{
    Console.WriteLine(el.GetAttribute(价值));
}
 
青铜到王者,看看你的MySQL数据库是什么段位,如何提升

How programmatically extract enumeration constraint values of an element from xsd schema file using .net?

for example I'd like to extract 'Audi', 'Golf' and 'BMW' from the following xsd:

<xs:element name="car">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:enumeration value="Audi"/>
      <xs:enumeration value="Golf"/>
      <xs:enumeration value="BMW"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

解决方案

There is an XmlSchema class, but it looks pretty... "fun" to work with.

Would xml querying be enough?

XmlDocument doc = new XmlDocument();
doc.Load("Foo.xsd");            
XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
mgr.AddNamespace("xx", "http://www.w3.org/2001/XMLSchema");
foreach (XmlElement el in doc.SelectNodes("//xx:element[@name='car'][1]/xx:simpleType/xx:restriction/xx:enumeration", mgr))
{
    Console.WriteLine(el.GetAttribute("value"));
}