How to parse Soap XML in Windows Phone 7 - c#

I have a XML Like Below, But i am not able to parse it. Please help me to parse the below XML.
<?xml version="1.0" encoding="utf-8"?><soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body>
<GetResponse xmlns="http://tempuri.org/"><GetResult>
<diffgr:diffgram>
<NewDataSet>
<Table>
<a>hi1</a>
<b>hi2</b>
</Table>
<Table>
<a>hi3</a>
<b>hi4</b>
</Table>
</NewDataSet>
</diffgr:diffgram>
</GetResponse></GetResult>
</soap:Body>
</soap:Envelope>
Here i want the results of the Table (i.e a,b ) tag. I tried using Linq but i am not able parse it. i tried the code some thing like this:
//XML will be there in response string
String response = e.response;
public static String myNamespace = "http://tempuri.org/";
XDocument reader = XDocument.Parse(response);
var results = from result in reader.Descendants(XName.Get("GetResponse", myNamespace))
select result.Element("GetResult").
But this code is returning null.
Thanks In advance.

In you c# project, you can import a Web Reference and make that point to the web service provider you are using. This reference will create an encapsulation class which will abstract all the SOAP protocol to you.
See the MSDN link for more details: http://msdn.microsoft.com/en-us/library/d9w023sx%28VS.71%29.aspx

Related

Deserialize an xml file or parse using linq to xml

Simplified XML file I need to decode:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:deliverylistResponse xmlns:ns2="http://tdriverap3.wsbeans.iseries/">
<return>
<COUNT>3</COUNT>
<DELIVERIES>
<ADD1>1300 address 1</ADD1>
<CITY>NICE CITY</CITY>
<ZIP>85705</ZIP>
</DELIVERIES>
<DELIVERIES>
<ADD1>40 S PINAL PKWY AVE</ADD1>
<CITY>FLORENCE</CITY>
<ZIP>85132</ZIP>
</DELIVERIES>
<DELIVERIES>
<ADD1>1825 EAST MAIN</ADD1>
<CITY>CHANDLER</CITY>
<ZIP>85286</ZIP>
</DELIVERIES>
<ERRORCODE/>
<RUNDATE>09/26/2018</RUNDATE>
</return>
</ns2:deliverylistResponse>
</soap:Body>
</soap:Envelope>
I am using the following to try and decode each of the individual addresses in the code.
I cannot figure out how to access them.
XElement xelement = XElement.Load(#"e:\test\X2.xml");
IEnumerable<XElement> addresses = xelement.Elements();
foreach (var address in addresses)
{
Console.WriteLine(address);
Console.WriteLine(address.Element("CITY").Value);
}
The first writeline works (it outputs the entire XML tree), the second says "System.Xml.Linq.XContainer.Element(...) returned null" - I have tried using DELIVERIES, COUNT, Body etc...
Obviously I am not telling it correctly how to traverse the structure, but I do not know how to go any further with it..
UPDATE: Thanks to some help I have figured out how to do it using Linq.
I would still like to be able to deserialize it if anybody has a pointer.
I followed several tutorials, but the multiple levels of this XML seems to be throwing me off.
I have created a class to hold the data but that is as far as my success with that path has gone.
Thank you,
Joe
Thank you Crowcoder -- this is what I wound up with, which will work.
The real XML file however does have about 60 fields so this is not as good as using a deserialize routine but I can at least move forward with the project.
XElement xelement = XElement.Load(#"e:\test\x2.xml");
IEnumerable<XElement> textSegs =
from seg in xelement.Descendants("DELIVERIES")
select seg;
foreach (var address in textSegs)
{
Console.WriteLine(address.Element("ADD1").Value);
Console.WriteLine(address.Element("CITY").Value);
Console.WriteLine(address.Element("ZIP").Value);
}
Console.ReadKey();

How to read the WSDL of a WCF service from a URL in C#

I'm trying to read a WSDL from a URL to dynamically generate the proxy for the WCF service.
This is my code:
XmlTextReader xmlTextReader = new XmlTextReader(new StringReader(description))
if (ServiceDescription.CanRead(xmlTextReader))
{
...
}
I get an XmlException from method ServiceDescription.CanRead.The error massage is "Data at the root level is invalid. Line 1, position 1".
Browsing the WDSL URL in IE, I can see the following tag at the start before tag <wsdl:definitions ...> ... </wsdl:definitions> which doesn't appear in chrome.
<?xml version="1.0" encoding="UTF-8"?>
Could that be the issue? but I suppose ServiceDescription.CanRead should be able to recognise that. Any hints would be appreciated.
Try adding this before the first line included in your question:
var byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());
if (description.StartsWith(byteOrderMarkUtf8))
{
var lastIndexOfUtf8 = byteOrderMarkUtf8.Length - 1;
description = description.Remove(0, lastIndexOfUtf8);
}
Borrowed from here.

Error while deserializing an XML document using RestSharp

I am trying to deserialize the following XML response using RestSharp:
<?xml version="1.0" encoding="UTF-8"?>
<ns0:payload xmlns:ns0="http://www.website.co.za/JSON_Token">
<ns0:content>
<ns0:reason>token successfully created</ns0:reason>
<ns0:success>true</ns0:success>
<ns0:authDetails>
<ns0:accessToken>feefaee94822a92ca7f134f74588cc69081b0e94</ns0:accessToken>
<ns0:expiresIn>604800</ns0:expiresIn>
<ns0:refreshToken>bc036cba4d346bf76809e143879cb8fb6983940c</ns0:refreshToken>
</ns0:authDetails>
</ns0:content>
This is a snapshot of my code:
IRestResponse response = client.Execute(request);
RestSharp.Deserializers.XmlDeserializer deserial = new RestSharp.Deserializers.XmlDeserializer();
payload apiresponse = deserial.Deserialize<payload>(response);
And this is the error that I am getting:
An unhandled exception of type 'System.Xml.XmlException' occurred in
System.Xml.dll Additional information: Data at the root level is
invalid. Line 1, position 1.
any ideas what I am doing wrong?
Thanks for all the replies.
I did some more investigation and after printing the content of the response to a string, it turned out that RestSharp was actually converting it from XML to JSON. No idea why it was doing that (i certainly wasn't specifying it, perhaps it's a default setting).
So because the response was a JSON then the XML deserializing was obviously throwing an error!
Thanks again.
Well, the exception message is pretty clear: Line 1 has invalid syntax:
<ns0:payload xmlns:ns0="http://www.website.co.za/JSON_Token">
The XML should probably look like this instead:
<?xml version="1.0" encoding="UTF-8"?>
<ns0:payload xmlns:ns0="http://www.website.co.za/JSON_Token">
<ns0:content>
<ns0:reason>token successfully created</ns0:reason>
<ns0:success>true</ns0:success>
<ns0:authDetails>
<ns0:accessToken>feefaee94822a92ca7f134f74588cc69081b0e94</ns0:accessToken>
<ns0:expiresIn>604800</ns0:expiresIn>
<ns0:refreshToken>bc036cba4d346bf76809e143879cb8fb6983940c</ns0:refreshToken>
</ns0:authDetails>
</ns0:content>
</ns0:payload>
If you cannot change how the XML response is generated, you should pre-process the XML using common string-manipulation since it is invalid XML and hence cannot be parsed using standard tools.

Lost prefix's definitions after deserialization of WCF message

I develop WCF-client. My client should validate incoming messages.
One of the messages has the following structure:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" xmlns:SOAP-ENC="http://www.w3.org/2003/05/soap-encoding" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Header>...</SOAP-ENV:Header>
<SOAP-ENV:Body>
<OpDescriptionResponse>
<Field Name="DateTime" Type="xsd:dateTime">
</OpDescriptionResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
In this case the client should validate: the field "DateTime" has type "dateTime" from namespace "http://www.w3.org/2001/XMLSchema".
This response is deserialized in structure containing array of XmlElement.
But I have an issue: after message is deserialized and I received corresponding variable, containing all Field nodes I can't determine value of prefix "xsd", i.e. if I take any elementof type XMLElement corresponding to Field node in reply and call element.GetNamespaceOfPrefix("xsd") I get empty string as result.
How can I get prefix's definitions are saved after deserialization?
Help me please to overcome this issue.
In order to influence the namespace/prefix, you need to use XmlSerializerNamespaces.
The following code provides a rough reference:
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add("prefixHere", "http://namespace.here/");
XmlSerializer tempSerializer = new XmlSerializer(messageObject.GetType());
tempSerializer.Serialize(Console.Out, messageObject, namespaces);
Regards,

Pivotviewer's .cxml parsing

I'm trying to do very simple operations on a .cxml file. As you know it's basically an .xml file. This is a sample file I created to test the application:
<?xml version="1.0" encoding="utf-8"?>
<Collection xmlns:p="http://schemas.microsoft.com/livelabs/pivot/collection/2009" SchemaVersion="1.0" Name="Actresses" xmlns="http://schemas.microsoft.com/collection/metadata/2009">
<FacetCategories>
<FacetCategory Name="Nationality" Type="LongString" p:IsFilterVisible="true" p:IsWordWheelVisible="true" p:IsMetaDataVisible="true" />
</FacetCategories>
<!-- Other entries-->
<Items ImgBase="Actresses_files\go144bwo.0ao.xml" HrefBase="http://www.imdb.com/name/">
<Item Id="2" Img="#2" Name="Anna Karina" Href="nm0439344/">
<Description> She is a nice girl</Description>
<Facets>
<Facet Name="Nationality">
<LongString Value="Danish" />
</Facet>
</Facets>
</Item>
</Items>
<!-- Other entries-->
</Collection>
I can't get any functioning simple code like:
XDocument document = XDocument.Parse(e.Result);
foreach (XElement x in document.Descendants("Item"))
{
...
}
The test on a generic xml is working. The cxml file is correctly loaded in document.
While watching the expression:
document.Descendants("Item"), results
the answer is:
Empty "Enumeration yielded no results" string
Any hint on what can be the error? I've also add a quick look to get Descendants of Facet, Facets, etc., but there are no results in the enumeration. This obviously doesn't happen with a generic xml file I used for testing. It's a problem I have with .cxml.
Basically your XML defines a default namespace with the xmlns="http://schemas.microsoft.com/collection/metadata/2009" attribute:
That means you need to fully qualify your Descendants query e.g.:
XDocument document = XDocument.Parse(e.Result);
foreach (XElement x in document.Descendants("{http://schemas.microsoft.com/collection/metadata/2009}Item"))
{
...
}
If you remove the default namespace from the XML your code actually works as-is, but that is not the aim of the exercise.
See Metadata.CXML project under http://github.com/Zoomicon/Metadata.CXML sourcecode for LINQ-based parsing of CXML files.
Also see ClipFlair.Metadata project at http://github.com/Zoomicon/ClipFlair.Metadata for parsing one's CXML custom facets too
BTW, at http://ClipFlair.codeplex.com can checkout the ClipFlair.Gallery project for how to author ASP.net web-based forms to edit metadata fragments (parts of CXML files) and merge them together in a single one (that you then convert periodically to DeepZoom CXML with PAuthor tool from http://pauthor.codeplex.com).
If anyone is interested in doing nesting (hierarchy) of CXML collections see
http://github.com/Zoomicon/Trafilm.Metadata
and
http://github.com/Zoomicon/Trafilm.Gallery

Categories

Resources