convert xdocument to IEnumerable<Dictionary<string, object>>

我怕爱的太早我们不能终老 提交于 2020-01-06 01:45:13

问题


I am faced with a problem in my service, one Dataset will fill and convert into a serialized XML object:

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();
}

This code return me a DataSet:

<?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>

Now I don't know how to read or convert into List<Dictionary<string, object>>, or any other object which I can bind into a Silverlight Datagrid.


回答1:


Not sure I understand exactly what you're asking, but you may want to try XDocument.Parse to load your document. Then use Linq to generate a list... I'm not going to type up everything for you, but at some point you may want to be doing something like this:

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();

Regards, Stephen



来源:https://stackoverflow.com/questions/7255882/convert-xdocument-to-ienumerabledictionarystring-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!