Reading XML from a web service
-
I'm having issue understanding how I can read XML from a webservice. What webservice is sending back an object array that contains XmlAttribute and XmlElements within the array. Most of what I am finding on how to parse an XML response they are either deserializing it and converting it to their custom class or they are loading from a file. In my case I can't do neither since what is returned is an object[]. I'm not asking for code.. just some help to point me in the right direction on what process I should take for reading this information.
-
I'm having issue understanding how I can read XML from a webservice. What webservice is sending back an object array that contains XmlAttribute and XmlElements within the array. Most of what I am finding on how to parse an XML response they are either deserializing it and converting it to their custom class or they are loading from a file. In my case I can't do neither since what is returned is an object[]. I'm not asking for code.. just some help to point me in the right direction on what process I should take for reading this information.
If the objects within the array are well-formed XML in strings, then you can use an XmlDocument and its LoadXml method then go from there. But maybe you need to read up more on deserialization.
-
If the objects within the array are well-formed XML in strings, then you can use an XmlDocument and its LoadXml method then go from there. But maybe you need to read up more on deserialization.
The problem is objects within the array are not strings. They are of type XmlAttribute and XmlElement. So this is the return I am supposed to get:
<response code="200" name="success">
<message>Domain domain.com exists</message>
</response>But it returns: object[] getData = soap.domainExists("domain.com"); So within getData is: getData[0] = XmlAttribute (type) getData[1] = XmlElement (type) getData[2] = XmlElement (type) getData[3] = XmlElement (type)
-
If the objects within the array are well-formed XML in strings, then you can use an XmlDocument and its LoadXml method then go from there. But maybe you need to read up more on deserialization.
I see. I did this:
object[] blah = soap.domainExists("domain.com", param);
foreach (object o in blah) { Type t = o.GetType(); Console.WriteLine(t); if (t == typeof(XmlElement)) Console.WriteLine(((XmlElement)o).InnerXml); else if (t == typeof(XmlAttribute)) Console.WriteLine(((XmlAttribute)o).InnerXml); }
Which returns:
status_code200
System.Xml.XmlElement
status_namesuccess
System.Xml.XmlElement
status_messageDomain domain.com existsI jsut need to figure out how to parse it
-
I'm having issue understanding how I can read XML from a webservice. What webservice is sending back an object array that contains XmlAttribute and XmlElements within the array. Most of what I am finding on how to parse an XML response they are either deserializing it and converting it to their custom class or they are loading from a file. In my case I can't do neither since what is returned is an object[]. I'm not asking for code.. just some help to point me in the right direction on what process I should take for reading this information.
JD86 wrote:
I'm having issue understanding how I can read XML from a webservice.
What webservice is sending back an object array that contains XmlAttribute and XmlElements within the array.From this and your other posts I am pretty sure your problem is that you want to 1. Get the xml 2. parse the xml 3. turn it into some sort of data structure. Steps 2 and 3 have nothng to do with a "webservice". Steps 2/3 are the same regardless of where the xml comes from. As far as a solution to 2/3 then the steps are. A. FIRST determine what is in the xml. B. Second learn how to parse xml. C. Write code using B and knowledge from A to produce what you want.
-
JD86 wrote:
I'm having issue understanding how I can read XML from a webservice.
What webservice is sending back an object array that contains XmlAttribute and XmlElements within the array.From this and your other posts I am pretty sure your problem is that you want to 1. Get the xml 2. parse the xml 3. turn it into some sort of data structure. Steps 2 and 3 have nothng to do with a "webservice". Steps 2/3 are the same regardless of where the xml comes from. As far as a solution to 2/3 then the steps are. A. FIRST determine what is in the xml. B. Second learn how to parse xml. C. Write code using B and knowledge from A to produce what you want.
Thanks. The problem I'm having is I'm expecting/wanting something in the format:
Domain domain.com exists
Which is what you get if you browse to the webpage. But I'm getting the format shown above which is completely different and more difficult to parse from what I can tell
-
I'm having issue understanding how I can read XML from a webservice. What webservice is sending back an object array that contains XmlAttribute and XmlElements within the array. Most of what I am finding on how to parse an XML response they are either deserializing it and converting it to their custom class or they are loading from a file. In my case I can't do neither since what is returned is an object[]. I'm not asking for code.. just some help to point me in the right direction on what process I should take for reading this information.
Ok I just parsed it my own way
<pre lang="c#">
object[] blah = soap.domainExists("domain.com", param);bool success = false; string msg = string.Empty; ParseXml(blah, out success, out msg);
</pre>
<pre lang="c#">
static void ParseXml(object[] data, out bool success, out string message)
{
success = false;
message = "Failed to parse XML";// Loop through objects foreach (object o in data) { // We only care about XmlElement. Ignore the XmlAttribute if (o is XmlElement) { // Get the innerText string text = ((XmlElement)o).InnerText; if (text.StartsWith("status\_code")) { if (text.Replace("status\_code", string.Empty).Equals("200", StringComparison.CurrentCultureIgnoreCase)) success = true; } else if (text.StartsWith("status\_message")) { message = text.Replace("status\_message", string.Empty); } } } }
</pre>