2012-09-06 51 views
0

我正在研究silverlight將xml文件綁定到datagrid。我發現了很多示例,但我的xml文件非常複雜。所以如何讀取或綁定到數據網格。 下面是我的XML文件,我想讀取具有其子元素及其屬性值的元素「EntityType」。將xml文件綁定到silverlight中的datagrid

謝謝。

`<?xml version="1.0" encoding="utf-8" ?> 
<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx"> 
    <edmx:DataServices xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:DataServiceVersion="1.0"> 
    <Schema Namespace="Microsoft.Crm.Sdk.Data.Services" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://schemas.microsoft.com/ado/2007/05/edm"> 
     <EntityType Name="SdkMessageRequestField"> 
     <Key> 
      <PropertyRef Name="SdkMessageRequestFieldId" /> 
     </Key> 
     <Property Name="FieldMask" Type="Edm.Int32" Nullable="true" /> 
     <Property Name="Name" Type="Edm.String" Nullable="true" /> 
     </EntityType> 
     <ComplexType Name="EntityReference"> 
     <Property Name="Id" Type="Edm.Guid" Nullable="true" /> 
     <Property Name="LogicalName" Type="Edm.String" Nullable="true" /> 
     <Property Name="Name" Type="Edm.String" Nullable="true" /> 
     </ComplexType> 
    </Schema> 
    </edmx:DataServices> 
</edmx:Edmx>` 

回答

1

XDocument x = XDocument.Load(「XMLFileName.xml」);

var a = (from c in x.Descendants("edmx").Elements("Schema").Elements("EntityType/ComplexType") 

      select new 
      { 
       Name = c.Parent.Attribute("Name"), 
       PropertyName = c.Attribute("Name"), 
       PropertyType = c.Attribute("Type") 
      }).ToArray(); 

    foreach (var itm in a) 
    { 
     // TODO:..... 
    }