簡體   English   中英

如何使用Linq從XML查詢xsi:type?

[英]How to query xsi:type from an attribute using Linq to XML?

鑒於此xml:

<?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 to XML代碼:

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

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

不幸的是,“var type = e.Attributes(”xsi:type“);”行不起作用,因為名稱中不允許使用冒號。

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

謝謝,

干草堆

XNamespace ns =“ http://www.w3.org/2001/XMLSchema-instance ”;

...

var type = e.Attributes(ns +“type”);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM