将xml文件绑定到silverlight中的datagrid

时间:2012-09-06 06:11:30

标签: silverlight silverlight-4.0

我正在使用silverlight将xml文件绑定到datagrid。我发现了许多示例,但我的xml文件非常复杂。所以如何读取或绑定到数据网格。 下面是我的xml文件,我想读取元素“EntityType”及其子元素及其属性值。

感谢。

`<?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 个答案:

答案 0 :(得分: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:..... 
    }