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

本文关键字:DataSet XML 响应 Web 服务 来自 | 更新日期: 2023-09-27 18:22:11

下面是我试图使用的有效且更新的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);

它不断抛出错误,我错过了"diffgr"。

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

作为respone XML得到的是一个已保存的数据集。与其试图破译它生成的diffgram,不如更好地利用该类型的ReadXml方法。它完全可以读取xml,并为您提供表和关系上的指定行为。

var ds = new DataSet();
ds.ReadXml(File.Open(@"C:'temp'ds.xml", FileMode.Open));
var listOfStatus = from row in ds.Tables["response"].Rows.Cast<DataRow>()
       from status in row.GetChildRows("response_status")
       select new { code = status["code"], 
                    value = status["value"], 
                    description = status["description"] };
foreach(var stat in listOfStatus)
{
    // do what ever you need to do with the Status record(s)
}

在上面的代码中,我读取了表响应中的行,然后获取表状态的子行。对于每个状态行,都会创建一个匿名类型,其中包含您似乎感兴趣的三个字段。您可以通过迭代该匿名类型的IEnumerable来处理这些字段。