【问题标题】:convert xdocument to IEnumerable<Dictionary<string, object>>将 xdocument 转换为 IEnumerable<Dictionary<string, object>>
【发布时间】:2011-08-31 10:49:37
【问题描述】:

我的服务遇到一个问题,一个数据集将填充并转换为序列化的 XML 对象:

string xmlString;
System.Xml.Serialization.XmlSerializer oSerializer = new System.Xml.Serialization.XmlSerializer(typeof(DataSet));
DataSet ds = new DataSet();
StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter(sb))
{
    oSerializer.Serialize(sw, ds);
    xmlString = sb.ToString();
}

这段代码返回给我一个数据集:

<?xml version="1.0" encoding="utf-16"?>
<DataSet>
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:Locale="">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="Table">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="ID" type="xs:int" minOccurs="0" />
                <xs:element name="CheckboxCol" type="xs:int" minOccurs="0" />
                <xs:element name="Last_x0020_Name" type="xs:string" minOccurs="0" />
                <xs:element name="First_x0020_Name" type="xs:string" minOccurs="0" />
                <xs:element name="User_x0020_Group" type="xs:string" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
    <NewDataSet>
      <Table diffgr:id="Table1" msdata:rowOrder="0">
        <ID>1</ID>
        <CheckboxCol>0</CheckboxCol>
        <Last_x0020_Name>patel</Last_x0020_Name>
        <First_x0020_Name>krunal</First_x0020_Name>
      </Table>
      <Table diffgr:id="Table2" msdata:rowOrder="1">
        <ID>46</ID>
        <CheckboxCol>0</CheckboxCol>
        <Last_x0020_Name>123</Last_x0020_Name>
        <First_x0020_Name>123</First_x0020_Name>
        <User_x0020_Group>123</User_x0020_Group>
      </Table>
      <Table diffgr:id="Table3" msdata:rowOrder="2">
        <ID>47</ID>
        <CheckboxCol>0</CheckboxCol>
        <Last_x0020_Name>def</Last_x0020_Name>
        <First_x0020_Name>abc</First_x0020_Name>
        <User_x0020_Group />
      </Table>
    </NewDataSet>
  </diffgr:diffgram>
</DataSet>

现在我不知道如何读取或转换为 List&lt;Dictionary&lt;string, object&gt;&gt; 或任何其他可以绑定到 Silverlight Datagrid 的对象。

【问题讨论】:

    标签: c# silverlight wcf silverlight-4.0


    【解决方案1】:

    不确定我是否完全理解您的要求,但您可能想尝试 XDocument.Parse 来加载您的文档。然后使用 Linq 生成一个列表......我不会为你输入所有内容,但在某些时候你可能想要做这样的事情:

    XDocument xdoc = XDocument.Parse(xmlString);
    ...
    ...
    List<Dictionary<string, object>> list = xdoc.Root.Elements().Where(e=>e.Name.LocalName == "diffgram").Select(col => new Dictionary<string, object>() { { col.Name.LocalName.ToString(), col.Value } }).ToList();
    

    问候, 斯蒂芬

    【讨论】:

    • 我觉得问题够具体,英文也够好。我见过更糟糕的情况,但设法明白了这一点。
    • 好吧...对不起。我不习惯在这里回答问题。想我今天试一试:)
    • 我收到错误 List&lt;Dictionary&lt;string, object&gt;&gt; list = myDoc.Root.Elements().Where(e =&gt; e.Name.LocalName == "diffgram").Select(col =&gt; new Dictionary&lt;string, object&gt;() { **new** KeyValuePair&lt;string, object&gt;() **{** col.Name, col.Value } }).ToList(); 方法“Add”没有重载需要 1 个参数无法使用集合初始化程序初始化类型“System.Collections.Generic.KeyValuePair”,因为它没有实现'System.Collections.IEnumerable'
    • 我已将代码更新为可以编译的版本。您还需要使用 System.Xml.Linq 和 System.Linq。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多