![](/img/trans.png)
[英]How to write a single LINQ to XML query to iterate through all the child elements & all the attributes of the child elements?
[英]XmlSchema inferred from an XML file - how to iterate through all the elements in the XSD?
我有一個XML文件,我在運行時使用XmlSchemaInference
類推斷其XSD架構。
樣本文件:
<products>
<product id="1" name="t-shirt">
<size name="medium"/>
<size name="large"/>
<price>
<net>10</net>
<gross>25</gross>
</price>
</product>
<product id="2" name="computer mouse">
<price>
<net>50</net>
</price>
</product>
</products>
它確實有效 - 它很好地推斷了架構:
<?xml version="1.0"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="products">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="product">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="size">
<xs:complexType>
<xs:attribute name="name" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
<xs:element name="price">
<xs:complexType>
<xs:sequence>
<xs:element name="net" type="xs:unsignedByte" />
<xs:element minOccurs="0" name="gross" type="xs:unsignedByte" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="id" type="xs:unsignedByte" use="required" />
<xs:attribute name="name" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
問題是:
如何迭代(遞歸?)遍歷此模式中的所有元素? 它們是如何由XmlSchemaSet
類存儲的? 我需要將它們呈現給用戶,以便他們可以進行一些映射。
我正在從XmlSchemaSet.Schemas
屬性中檢索XmlSchema
,然后是什么? XmlSchema.Elements
只包含一個項目( products
),我找不到任何方法來查找其子元素。
好的! 沒有答案也沒有太多興趣 - 我自己想出來了。
我使用了這篇MSDN文章中的代碼我搜索過: 遍歷XML Schema
我基於它的遞歸解決方案。
void PrintSchema(string xmlFilePath)
{
var schemaSet = new XmlSchemaInference().InferSchema(XmlReader.Create(xmlFilePath));
foreach (XmlSchemaElement element in schemaSet
.Schemas()
.Cast<XmlSchema>()
.SelectMany(s => s.Elements.Values.Cast<XmlSchemaElement>()))
{
Debug.WriteLine(element.Name + " (element)");
IterateOverElement(element.Name, element);
}
}
void IterateOverElement(string root, XmlSchemaElement element)
{
var complexType = element.ElementSchemaType as XmlSchemaComplexType;
if (complexType == null)
{
return;
}
if (complexType.AttributeUses.Count > 0)
{
var enumerator = complexType.AttributeUses.GetEnumerator();
while (enumerator.MoveNext())
{
var attribute = (XmlSchemaAttribute)enumerator.Value;
Debug.WriteLine(root + "." + attribute.Name + " (attribute)");
}
}
var sequence = complexType.ContentTypeParticle as XmlSchemaSequence;
if (sequence == null)
{
return;
}
foreach (XmlSchemaElement childElement in sequence.Items)
{
root += String.Concat(".", childElement.Name);
Debug.WriteLine(root + " (element)");
// recursion
IterateOverElement(root, childElement);
}
}
輸出是:
products (element)
products.product (element)
products.product.id (attribute)
products.product.name (attribute)
products.product.size (element)
products.product.size.name (attribute)
products.product.price (element)
products.product.price.net (element)
products.product.price.gross (element)
我留給你判斷這個API是多么友好,特別是考慮到這些特定類的MSDN文檔有多么稀缺。 任何意見或見解表示贊賞。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.