Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

access sub XML values in sms web service that hasnt value in standard way

I have following xml :

in other standard xml like this question here we have no problem but in php web service :

http://sandoghche.com/WebService

for inbox check my inbox xml is this:

<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
<Result>
  <text_message>
    <id>509</id>
    <message><![CDATA[a]]></message>
    <time>1323519941</time>
    <message_phone>
      <cellphone>09196070718</cellphone>
    </message_phone>
  </text_message>
  <text_message>
    <id>507</id>
    <message>1</message>
    <time>1323519803</time>
    <message_phone>
      <cellphone>09360437392</cellphone>
    </message_phone>
  </text_message>
  <text_message>
    <id>303</id>
    <message><![CDATA[smsm text]]></message>
    <time>1318343296</time>
    <message_phone>
      <cellphone>09354338365</cellphone>
    </message_phone>
  </text_message>
  <text_message>
    <id>219</id>
    <message><![CDATA[my sms texts here]]></message>
    <time>1316154042</time>
    <message_phone>
      <cellphone>09127930265</cellphone>
    </message_phone>
  </text_message>
  <text_message>
    <id>217</id>
    <message>2</message>
    <time>1316090189</time>
    <message_phone>
      <cellphone>09195533234</cellphone>
    </message_phone>
  </text_message>
</Result>

for this I wrote the following code

public class SMSData
{
    public int id { set; get; }
    public string message { set; get; }
    public string cellphone { set; get; }
}



  string data = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><Result><text_message><id>509</id><message><![CDATA[a]]></message><time>1323519941</time><message_phone><cellphone>09196070718</cellphone></message_phone></text_message><text_message><id>507</id><message>1</message><time>1323519803</time><message_phone><cellphone>09360437392</cellphone></message_phone></text_message><text_message><id>303</id><message><![CDATA[ممنون.شما؟]]></message><time>1318343296</time><message_phone><cellphone>09354378365</cellphone></message_phone></text_message><text_message><id>219</id><message><![CDATA[سلام اقاي طباطبايي لينك مربوط به ورود اعضا به وبلاگ روي صفحه فيلتر مي رود با احترام احدي]]></message><time>1316154042</time><message_phone><cellphone>09127960265</cellphone></message_phone></text_message><text_message><id>217</id><message>2</message><time>1316090189</time><message_phone><cellphone>09195523234</cellphone></message_phone></text_message></Result>";

    Stream s = GenerateStreamFromString(data);

    XmlReader xr = XmlReader.Create(s);

        while (xr.Read())
        {
            if ((xr.NodeType == XmlNodeType.Element) && (xr.Name == "text_message"))
            {
                if (xr.HasValue)
                {
                    string id = xr.GetAttribute("id");
                    string message = xr.GetAttribute("message");
                }
            }
        }

and this code

    Dictionary<int, string> myDictionary = new Dictionary<int, string>();
    XmlDocument xml = new XmlDocument();
    xml.LoadXml(data);
    XmlNodeList xnList = xml.SelectNodes("/Result");

    foreach (XmlNode xn in xnList)
    {

but can't catch values on this xml document.

Is there a clean way to do this?

like image 422
saber tabatabaee yazdi Avatar asked Aug 04 '14 15:08

saber tabatabaee yazdi


People also ask

What is the value of XML technology in web services?

The value of XML technology is that it enables computer systems that are completely foreign to each other to communicate in a common language. When using web services, this transfer of XML data occurs across the enterprise or across the Internet using the HTTP protocol.

How do I control the XML generated by an XML Web service?

To control the XML generated by an XML Web service, you can apply the attributes listed in both Attributes That Control XML Serialization and Attributes That Control Encoded SOAP Serialization to the classes, return values, parameters, and fields of a file used to create an XML Web service (.asmx).

How do I send and receive relational data using XML Web Services?

The DataSet is implicitly converted to an XML stream using the DiffGram format, sent over the network, and then reconstructed from the XML stream as a DataSet on the receiving end. This gives you a very simple and flexible method for transmitting and returning relational data using XML Web services.

How do I read an XML file from a web service?

First, you will work with the Web Service Task to interact with a public web service. Second, you will use the XML Source adapter to extract data from an XML document embedded in a file. In one of the web service examples, you will also use the XML Task to read the XML file.


1 Answers

It is easier to use the built-in Serializers for these kind of things, like the XmlSerializer or the DataContractSerializer. I used the latter and tested your XML with the following snippet.

First annotate your SMSData class and create a new class to hold the Message_phone elements in a new class:

[DataContract(Name="text_message",Namespace = "")]
public class SMSData
{
    [DataMember(Order =1)]
    public int id { set; get; }    
    [DataMember(Order = 2)]
    public string message { set; get; }
    [DataMember(Order = 3)]
    public int time { set; get; }
    [DataMember(Order = 4)]
    public Message_phone message_phone { set; get; }

     // for easy access to cellphone
        public string cellphone {
            get 
            {
                 return message_phone!=null?message_phone.cellphone:null;
            }
        }
}


[DataContract(Name="message_phone",Namespace = "")]
public class Message_phone
{
    [DataMember]
    public string cellphone { set; get; }
}

and then create an instance of the DataContractSerializer for your array of SMSData, and call ReadObject on it like so:

Stream s = GenerateStreamFromString(data);

var ds = new DataContractSerializer(
               typeof(SMSData[]),   // array of SMSdata
               "Result",            // top level element
               "",                  // empty namespace
               new List<Type> { typeof(SMSData[])} );    

var smsdata = (SMSData[]) ds.ReadObject(XmlReader.Create(s));

// smsdata now holds the SMSData from the Xml file
like image 164
rene Avatar answered Oct 24 '22 05:10

rene