简体   繁体   English

来自Web服务使用的DataSet XML响应

[英]DataSet XML Response from Web Service consumption

Below is the valid and updated XML I was trying to consume: 以下是我尝试使用的有效和更新的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>

With the C# code below: 使用下面的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);

It keeps throwing error that i missed "diffgr". 它一直抛出我错过了“ diffgr”的错误。

What you get as a respsone XML is a saved DataSet . 作为respsone XML获得的是保存的DataSet Instead of trying to decipher the diffgram it generates you better leverage the ReadXml method of that type. 与其尝试破译它所生成的diffgram ,不如您更好地利用该类型的ReadXml方法。 It is fully equipped to read the xml and gives you specified behavior on the Tables and Relationships . 它具备读取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)
}

In the above code I read the rows in the table response and then fetch the childrows for the table status. 在上面的代码中,我读取了表响应中的行,然后获取了表状态的子行。 For every status row an anonymous type is created with the three fields you seem to be interested in. You can process those by iterating over the IEnumerable of that anonymous type. 对于每个状态行,都会使用您似乎感兴趣的三个字段创建一个匿名类型。您可以通过迭代该匿名类型的IEnumerable来处理这些字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM