从wcf Web服务读取xml返回

时间:2014-03-07 09:28:44

标签: c# xml wcf dataset

我有一个课程

public class SelectedRoomAvailability
    {
        public DataTable CardType { get; set; }
        public DataTable CountryList { get; set; }
    }

以及以xml格式返回输出的方法

public List<SelectedRoomAvailability> RoomAvailability(params...)
{
}

下面是我得到的输出

 <RoomAvailabilityResponse xmlns="http://webservice.hotel.com.au/">


     <RoomAvailabilityResult xmlns:a="http://schemas.datacontract.org/2004/07/HotelWcfService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <a:ListRequest.SelectedRoomAvailability>
      <a:CardType>
        <xs:schema id="NewDataSet" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
          <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="EANCard" msdata:UseCurrentLocale="true">
            <xs:complexType>
              <xs:choice minOccurs="0" maxOccurs="unbounded">
                <xs:element name="EANCard">
                  <xs:complexType>
                    <xs:sequence>
                      <xs:element name="TypeName" type="xs:string" minOccurs="0" />
                      <xs:element name="Code" type="xs:string" minOccurs="0" />
                    </xs:sequence>
                  </xs:complexType>
                </xs:element>
              </xs:choice>
            </xs:complexType>
          </xs:element>
        </xs:schema>
        <diffgr:diffgram xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
          <DocumentElement xmlns="">
            <EANCard diffgr:id="EANCard1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
              <TypeName>Visa</TypeName>
              <Code>VI</Code>
            </EANCard>
            <EANCard diffgr:id="EANCard2" msdata:rowOrder="1" diffgr:hasChanges="inserted">
              <TypeName>MasterCard</TypeName>
              <Code>CA</Code>
            </EANCard>
            <EANCard diffgr:id="EANCard3" msdata:rowOrder="2" diffgr:hasChanges="inserted">
              <TypeName>American Express</TypeName>
              <Code>AX</Code>
            </EANCard>
            <EANCard diffgr:id="EANCard4" msdata:rowOrder="3" diffgr:hasChanges="inserted">
              <TypeName>Diners Club</TypeName>
              <Code>DC</Code>
            </EANCard>
          </DocumentElement>
        </diffgr:diffgram>
      </a:CardType>
      <a:CountryList>
        <xs:schema id="NewDataSet" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
          <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="Table" msdata:Locale="">
            <xs:complexType>
              <xs:choice minOccurs="0" maxOccurs="unbounded">
                <xs:element name="Table">
                  <xs:complexType>
                    <xs:sequence>
                      <xs:element name="CODE" type="xs:string" minOccurs="0" />
                      <xs:element name="Country" type="xs:string" minOccurs="0" />
                    </xs:sequence>
                  </xs:complexType>
                </xs:element>
              </xs:choice>
            </xs:complexType>
          </xs:element>
        </xs:schema>
        <diffgr:diffgram xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
          <NewDataSet xmlns="">
            <Table diffgr:id="Table1" msdata:rowOrder="0">
              <CODE>AX</CODE>
              <Country>?land Islands</Country>
            </Table>
            <Table diffgr:id="Table2" msdata:rowOrder="1">
              <CODE>AF</CODE>
              <Country>Afghanistan</Country>
            </Table>
            <Table diffgr:id="Table3" msdata:rowOrder="2">
              <CODE>AL</CODE>
              <Country>Albania</Country>
            </Table>
            <Table diffgr:id="Table4" msdata:rowOrder="3">
              <CODE>DZ</CODE>
              <Country>Algeria</Country>
            </Table>
            <Table diffgr:id="Table5" msdata:rowOrder="4">
              <CODE>AS</CODE>
              <Country>American Samoa</Country>
            </Table>
            <Table diffgr:id="Table6" msdata:rowOrder="5">
              <CODE>AD</CODE>
              <Country>Andorra</Country>
            </Table>
            <Table diffgr:id="Table7" msdata:rowOrder="6">
              <CODE>AO</CODE>
              <Country>Angola</Country>
            </Table>
            <Table diffgr:id="Table8" msdata:rowOrder="7">
              <CODE>AI</CODE>
              <Country>Anguilla</Country>
            </Table>
            <Table diffgr:id="Table9" msdata:rowOrder="8">
              <CODE>AQ</CODE>
              <Country>Antarctica</Country>
            </Table>
            <Table diffgr:id="Table10" msdata:rowOrder="9">
              <CODE>AG</CODE>
              <Country>Antigua And Barbuda</Country>
            </Table>
            <Table diffgr:id="Table11" msdata:rowOrder="10">
              <CODE>AR</CODE>
              <Country>Argentina</Country>
            </Table>
            <Table diffgr:id="Table12" msdata:rowOrder="11">
              <CODE>AM</CODE>
              <Country>Armenia</Country>
            </Table>
            <Table diffgr:id="Table13" msdata:rowOrder="12">
              <CODE>AW</CODE>
              <Country>Aruba</Country>
            </Table>
            <Table diffgr:id="Table14" msdata:rowOrder="13">
              <CODE>AU</CODE>
              <Country>Australia</Country>
            </Table>
            <Table diffgr:id="Table15" msdata:rowOrder="14">
              <CODE>AT</CODE>
              <Country>Austria</Country>
            </Table>
            <Table diffgr:id="Table16" msdata:rowOrder="15">
              <CODE>AZ</CODE>
              <Country>Azerbaijan</Country>
            </Table>
            <Table diffgr:id="Table17" msdata:rowOrder="16">
              <CODE>BS</CODE>
              <Country>Bahamas</Country>
            </Table>
            <Table diffgr:id="Table18" msdata:rowOrder="17">
              <CODE>BH</CODE>
              <Country>Bahrain</Country>
            </Table>
            <Table diffgr:id="Table19" msdata:rowOrder="18">
              <CODE>BD</CODE>
              <Country>Bangladesh</Country>
            </Table>
            <Table diffgr:id="Table20" msdata:rowOrder="19">
              <CODE>BB</CODE>
              <Country>Barbados</Country>
            </Table>
            <Table diffgr:id="Table21" msdata:rowOrder="20">
              <CODE>BY</CODE>
              <Country>Belarus</Country>
            </Table>
          </NewDataSet>
        </diffgr:diffgram>
      </a:CountryList>
    </a:ListRequest.SelectedRoomAvailability>
  </RoomAvailabilityResult>
</RoomAvailabilityResponse>

我使用以下代码来阅读此xml

var request = WebRequest.Create(webserviceurl);
        request.Method = "GET";
        request.ContentType = "application/XML";
        String responseFromServer = "No response from server";
        using (var response = (HttpWebResponse)request.GetResponse())
        {
            using (var dataStream = response.GetResponseStream())
            {
                if (dataStream != null)
                {
                    using (var reader = new StreamReader(dataStream))
                    {
                        responseFromServer = reader.ReadToEnd();
                    }
                }
            }
        }

XmlDocument doc = new XmlDocument();
        doc.LoadXml(responseFromServer);            


XmlNodeList xnl = doc.GetElementsByTagName("a:CardType");


DataSet dt = new DataSet();
            dt.ReadXmlSchema("<?xml version=\"1.0\" encoding=\"utf-8\"?><NewDataSet>" + xnl.Item(0).InnerXml + "</NewDataSet>");
            dt.ReadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?><NewDataSet>" + xnl.Item(0).InnerXml + "</NewDataSet>", XmlReadMode.IgnoreSchema);

我google了很多,发现上面的代码,但它不起作用。我从最近2天开始面对这个问题,最后我来找你们,请帮助我。

我想从输出中绑定数据集,以便根据我的要求使用该数据集。

我在博客中看到输出来自序列化,我试图反序列化,但即使在那里我也失败了。它也不允许我这样做。

请大家帮我读一下这个xml并将其存储到数据集或数据表或字典中,c#对象支持的任何内容。

提前完成了......

0 个答案:

没有答案