Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Developer,

I have a class below

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


and the method that returns the output in xml format

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


below is the output that i get

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



I use the following code to read this 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);


i googled a lot and found above code but its not working. i m facing this problem since last 2 days and at the end i come to you guys please help me.

i want to bind dataset from my output so i can use that dataset according to my requirement.

I read in blog that the output come by serialized and i tried to deserialized that but even there i m fail. its not allowing me to do that too.

please guys help me to read this xml and store that into dataset or datatable or dictionary, anything that c# object supports.

Thanx in advance...
Posted

1 solution

Hi there,
In many case I work with DataSet & XML. So here some tips:
Put the SerializableAttribute on your clases:
C#
[Serializable]
public class SelectedRoomAvailability
{
    public DataTable CardType { get; set; }
    public DataTable CountryList { get; set; }
}

C#
[Serializable]
public List<selectedroomavailability> RoomAvailability(params...)
{
}
</selectedroomavailability>

After that in your code to read XML, after DataSet's declaration you can put this:
C#
DataSet dt = new DataSet();
dt.ReadXml("" + xnl.Item(0).InnerXml + "", XmlReadMode.Auto);

So, Let ReadMode do the work for you. After that you will have your DataSet.

Hope it helps.
 
Share this answer
 
Comments
Member 10359492 7-Mar-14 23:57pm    
thanx dude! i did the way you suggest but still its not working if you have any new approach to do the same then plz suggest me...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900