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,
Related
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();
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.
I am getting an XML from a OAI-PMH request which contains a resumption token to get next set of records. How to get the resumption token Value and its other attributes such as completeListSize etc.
<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="static/style.xsl"?><OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd"><responseDate>2015-06-24T16:45:25Z</responseDate>
<request verb="ListRecords" metadataPrefix="uketd_dc">http://publications.iadb.org/oai/request</request>
<ListRecords>
<record>
<header>
<identifier>oai:publications.iadb.org:11319/195</identifier>
<datestamp>2015-06-12T23:02:48Z</datestamp>
<setSpec>com_123456789_1</setSpec>
<setSpec>col_123456789_3</setSpec>
</header>
<metadata></metadata>
</record>
<resumptionToken completeListSize="6305" cursor="0">MToxMDB8Mjp8Mzp8NDp8NTp1a2V0ZF9kYw==</resumptionToken>
</ListRecords>
I tried the below code
XDocument root= XDocument .Load("http://publications.iadb.org/oai/request?verb=ListRecords&metadataPrefix=uketd_dc");
var tokenValue= root.Element("resumptionToken").Value;
Its returning object reference error.Please help.
Element returns an immediate child element. From the context of your document, the only element available to you is OAI-PMH - this is why you're getting the null reference exception.
In addition, your target element has a namespace as defined by the default namespace declaration (xmlns="...") in the root OAI-PMH element.
You can use Descendants to find any element in the document with your required name, so this short code should work:
XNamespace ns = "http://www.openarchives.org/OAI/2.0/";
var tokenValue = (string)root.Descendants(ns + "resumptionToken").Single();
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
I am trying to get the data from input xml message using functoids. But this doesn't seem to work. Below is my XML snippet
<?xml version="1.0" ?>
<ROOT>
<COMPANIES>
<COMPANY>
<NAME>FOO CORP</name>
</COMPANY>
<COMPANY>
<NAME>ACME CORP</name>
</COMPANY>
</COMPANIES>
<INFORMATIONS>
<INFORMATION>
<TESTING>
<TESTS>
<NAME>1221</NAME>
<TEST>
<TEXT>I AM SAM</TEXT>
</TEST>
</TESTS>
<TESTS>
<NAME>21</NAME>
<TEST>
<TEXT>FADFDF</TEXT>
</TEST>
</TESTS>
<TESTS>
<NAME>3001</NAME>
<TEST>
<TEXT>SGFGSDFG</TEXT>
</TEST>
</TESTS>
<TESTS>
<NAME>4569</NAME>
<TEST>
<TEXT>12312</TEXT>
</TEST>
</TESTS>
</TESTING>
<INFORMATION>
</INFORMATIONS>
</ROOT>
First I am trying to loop through COMPANY and get the NAME "ACME CORP". but which ever functoid i use (scripting, string extract etc) I always get the FOO CORP. I even tried inline XSLT but that also doesn't seem to work. Any idea how to get the 2nd COMPANY name?
Secondly, I have mapped <TEXT> directly to <node> in my destination schema. What i get is only 2 values from <TEXT>. Not all the <TEXT> are mapped to my <node>. Output i am getting is
I don't get is
I AM SAM
FADFDF
I don't get
<node>SGFGSDFG</node>
<node>12312</node>
Any one knows how i could get the values?
Thanks in advance
cheers,
Karthik
Q1: How to get the NAME of the second COMPANY?
1) Add Iteration functoid (Advanced Functoids) and connect to COMPANY node in your source schema
2) Add Equal functiod (Logical Functoids) and connect to Iteration functoid
3) Configure Equal functoid; add constant value 2
4) Add Value Mapping (Flattening) functoid (Advanced Functoids)
5) Connect output of Equal to input of Value Mapping
6) Connect NAME emlement of source schema to input of Value Mapping
7) Connect output of Value Mapping to destination schema element
Q2: Not all source TEXT elements are transformed to destination node
This seems to be a side effect of your mapping concering Q1 and I cannot reproduce your error.
Check minOccurs and maxOccurs of your source and destination schema for the elements/nodes in question.