2010-01-27 42 views
7

鑑於此xml:如何從使用Linq到XML的屬性查詢xsi:type?

<?xml version="1.0" encoding="utf-8"?> 
<EntityDefinition xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <components> 
    <component xsi:type="TypeA"> 
     <Property1>100</Property1> 
    </component> 
    <component xsi:type="TypeB"> 
     <Property2>100</Property2> 
    </component> 
    </components> 
</EntityDefinition> 

我想上的部件循環並實例基於所述的xsi每個對象:type屬性。

下面是一些LINQ到XML代碼:

IEnumerable<XElement> components = 
    from c in elementsFromFile.Descendants("component") 
    select (XElement)c; 

    foreach (XElement e in components) 
    { 
     var type = e.Attributes("xsi:type"); 
    } 

遺憾的是,行「變種類型= e.Attributes( 「XSI:類型」);」不工作,因爲冒號不是在允許的名稱。

關於如何從每個元素查詢xsi:type屬性的想法?

謝謝

裏克

回答