来自Web服务消耗的DataSet XML响应

时间:2015-11-17 13:14:51

标签: c# asp.net xml dataset

以下是我尝试使用的有效且更新的XML:

<DataSet xmlns="http://tempuri.org/">
    <xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="NewDataSet">
    <xs:element name="response">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="user" minOccurs="0" maxOccurs="unbounded">
    <xs:complexType>
    <xs:attribute name="id" type="xs:string"/>
    </xs:complexType>
    </xs:element>
    <xs:element name="status" minOccurs="0" maxOccurs="unbounded">
    <xs:complexType>
    <xs:attribute name="code" type="xs:string"/>
    <xs:attribute name="value" type="xs:string"/>
    <xs:attribute name="description" type="xs:string"/>
    </xs:complexType>
    </xs:element>
    <xs:element name="error" minOccurs="0" maxOccurs="unbounded">
    <xs:complexType>
    <xs:attribute name="code" type="xs:string"/>
    <xs:attribute name="message" type="xs:string"/>
    </xs:complexType>
    </xs:element>
    </xs:sequence>
    <xs:attribute name="operation" type="xs:string"/>
    <xs:attribute name="timestamp" type="xs:string"/>
    </xs:complexType>
    </xs:element>
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
    <xs:choice minOccurs="0" maxOccurs="unbounded">
    <xs:element ref="response"/>
    </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 xmlns="">
    <response diffgr:id="response1" msdata:rowOrder="0" diffgr:hasChanges="inserted" operation="AUTHENTICATION" timestamp="2015-11-19 18:21:17.457" msdata:hiddenresponse_Id="0">
    <user diffgr:id="user1" msdata:rowOrder="0" diffgr:hasChanges="inserted" id="blumaesnetwork" msdata:hiddenresponse_Id="0"/>
    <status diffgr:id="status1" msdata:rowOrder="0" diffgr:hasChanges="inserted" code="315" value="FAILED" description="Authentication Failed. User ID Not Found" msdata:hiddenresponse_Id="0"/>
    <error diffgr:id="error1" msdata:rowOrder="0" diffgr:hasChanges="inserted" code="-1" message="User Not Found" msdata:hiddenresponse_Id="0"/>
    </response>
    </NewDataSet>
    </diffgr:diffgram>
    </DataSet>

使用下面的C#代码:

x.LoadXml(_xmlString);

XDocument x1 = new XDocument();
x1 = XDocument.Parse(_xmlString);

IEnumerable<responseStatus> ListRsts = (from e in x1.Descendants("responseStatus")
                                        select new responseStatus
                                        {
                                           code = e.Element("code").Value,
                                           value = e.Element("value").Value,
                                           description = e.Element("description").Value
                                        });
foreach (var br in ListRsts)
         codeField = (br.code);

它不断抛出我错过的错误&#34; diffgr&#34;。

1 个答案:

答案 0 :(得分:0)

您作为respsone XML获得的内容是已保存的DataSet。不要试图破译它生成的diffgram,而是更好地利用该类型的ReadXml方法。它完全可以读取xml并在表和Relationships上为您指定行为。

&

在上面的代码中,我读取了表响应中的行,然后获取表状态的子节点。对于每个状态行,使用您似乎感兴趣的三个字段创建匿名类型。您可以通过迭代该匿名类型的IEnumerable来处理它们。