2010-01-27 45 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属性的想法?

谢谢

里克

回答