I want to remove the backslashes from the soap service response.
Is there any way to remove the single backslashes?
string;
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">
Related
My programme parses an XML-File with:
require(XML)
data <- xmlTreeParse("excel_output.xml")
xml_data <- xmlToList(data)
The XML-File looks like this:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Meldung xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Tarifmeldung.xsd">
<somecontent></somecontent>
</Meldung>
Then I manipulate the content of the file. More specific I add a Base64-encoded file. In the last part of my programme I save the XML-File:
data <- xmlRoot(data)
saveXML(data, file = 'r_output.xml')
However, this process changes the second line of the XML:
<?xml version="1.0"?>
<Meldung noNamespaceSchemaLocation="Tarifmeldung.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<manipulated content> </manipulatedcontent
</Meldung>
By saving, the xsi: part of the "SchemaLocation" is dropped and the order of the to attributes is changed. When passing the File to a webservice this causes the service to reject my file.
Being an absolute beginner for XML-Files in R my question is: How can I prevent R from doing this an what am I doing wrong that this happens.
Thank you very much!
To keep namespace information you can use argument addAttributeNamespaces = TRUE and to keep order of elements useInternalNodes = TRUE :
require(XML)
xmldoc <-'<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Meldung xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Tarifmeldung.xsd">
<somecontent></somecontent>
</Meldung>'
data <- xmlTreeParse(xmldoc, addAttributeNamespaces = TRUE ,useInternalNodes = TRUE)
xml_data <- xmlToList(data)
data <- xmlRoot(data)
XML::saveXML(data)
#> [1] "<Meldung xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"Tarifmeldung.xsd\">\n <somecontent/>\n</Meldung>"
This is the response
"<?xml version='1.0' encoding='UTF-8'?>
<error xmlns=\"http://docs.oasis-open.org/odata/ns/metadata\">
<code>null</code>
<message>Resource - ID not found</message>
</error>"
And I need to parse the string that is coming in tag.
You're looking for an XML parser. The XML package's xmlParse function does a fair job:
XML::xmlToList(XML::xmlParse("<?xml version='1.0' encoding='UTF-8'?>
<error xmlns=\"http://docs.oasis-open.org/odata/ns/metadata\">
<code>null</code>
<message>Resource - ID not found</message>
</error>"))$message
#> [1] "Resource - ID not found"
I have a valid sitemap.xml file. The problem arises when I try to serve this file as a sitemap.xml. I get the following error:
This page contains the following errors:
error on line 1 at column 95: Extra content at the end of the document
Below is a rendering of the page up to the first error.
When I inspect /sitemap.xml from browser each element tag gets this added to it.
<url xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
the rest
</url>
Here is how I return the file from the controller:
XmlDocument xml = new XmlDocument();
xml.Load(#"C:\sitemap.xml");
return Content(xml.DocumentElement.InnerXml, "application/xml");
Here is an example of the file I have and trying to return
<?xml version="1.0" encoding="utf-8"?>
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
<loc>LINK</loc>
</url>
THE REST OF URLS
</urlset>
I have tried switching the "application/xml" to "text/xml" but didn't solve this problem. Am I not using XmlDocument correctly or am I not fully understanding what happens with return Content()?
Any help is appreciated.
Thank you
What ended up fixed this was a simple fix.
XmlDocument xml = new XmlDocument();
xml.Load(#"C:\sitemap.xml");
return Content(xml.DocumentElement.InnerXml, "application/xml");
Changed to
XmlDocument xml = new XmlDocument();
xml.Load(#"C:\sitemap.xml");
return Content(xml.DocumentElement.OuterXml, "application/xml");
Hope this helps someone later.
I've got a ASP.NET WebService that looks something like this:
[WebMethod]
public static void DoSomethingWithStrings(string stringA, string stringB)
{
// and so on
}
An third party application should call this webservice. However this application encodes strings as UTF-8 and all umlauts are replaced by '??'. I can view the call and the special characters are formatted well:
<?xml version="1.0" encoding="utf-8" ?>
<!-- ... -->
<SoapCall>
<DoSomethingWithStrings>
<stringA>Ä - Ö - Ü</stringA>
<stringB>This is a test</stringB>
</DoSomethingWithStrings>
</SoapCall>
This produces the following output, when I simply print the strings inside the webservice method:
?? - ?? - ??
This is a test
How can I configure the WebService to accept UTF-8 encoded strings?
Update
Fiddler also tells me that the content-type charset of the http request is UTF-8.
Update 2
I tried to add following code to global.asax for debugging purposes:
public void Application_BeginRequest(object sender, EventArgs e)
{
using (var reader = new System.IO.StreamReader(Request.InputStream))
{
string str = reader.ReadToEnd();
}
}
This reads the actual SOAP call. The StreamReaders encoding is set to UTF-8. The SOAP call looks correct:
<?xml version="1.0" encoding="UTF-8" ?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<DoSomethingWithStrings xmlns="http://www.tempuri.org/">
<stringA>Ä - Ö - Ü</stringA>
<stringB>This is a test!</stringB>
</DoSomethingWithStrings>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
In the web.config file the globalization settings are set correctly:
<globalization requestEncoding="UTF-8" responseEncoding="UTF-8" culture="de-DE" uiCulture="de-DE" />
So it looks like something that deserializes the SOAP message does not use UTF-8 but ASCII encoding.
Finally it turns out that something went wrong within accepting HTTP-Messages. I don't actually know what manipulates the HTTP-Request, but I found a workaround for this. Eventhough Fiddler showed me the correct content type (text/xml; charset=utf-8) in my Application_BeginRequest the Request.RequestContext.HttpContext.Request.ContentType was just text/xml, which lead to a fallback to default (ASCII) encoding within the ASMX serializer. I've added the following code to the Application_BeginRequest handler and everything works for now.
if (Request.RequestContext.HttpContext.Request.ContentType.Equals("text/xml"))
{
Request.RequestContext.HttpContext.Request.ContentType = "text/xml; charset=UTF-8";
}
Thanks for your help!
Try this:-
byte[] bytes=Encoding.UTF8.GetBytes(yourString);
NOTE:-
Strings never contain anything utf-* or anything else encoded
The SOAP call is being decoded as ASCII somewhere - each of the umlauts are 2 bytes with high bit being set, which turns into ?? when decoded as ASCII.
So, something like this is happening:
byte[] bytesSentFromClient = Encoding.UTF8.GetBytes("Ä - Ö - Ü");
string theStringIThenReceiveInMyMethod = Encoding.ASCII.GetString(bytesSentFromClient);
Console.WriteLine(theStringIThenReceiveInMyMethod);
//?? - ?? - ??
To verify this is happening for sure, you should compare stringA == "Ä - Ö - Ü" rather than printing it somewhere.
I guess you could start by doing a project-wide search for "ASCII" and then work from there if you find anything.
You could also try
<globalization requestEncoding="utf-8" responseEncoding="utf-8"/>
Under the <system.web> tag in Web.config file.
I had the same problem. Asmx web service converted my UTF-8 to ASCII or, better to say to ??????. Your post helped me a lot.
The solution I found was to change version of SOAP protocol from 1.1 to 1.2
I mean:
POST /WebService1.asmx HTTP/1.1
Host: www.tempuri.org
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.tempuri.org/HelloWorld"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<HelloWorld xmlns="http://www.tempuri.org/">
<inputParam>Привет</inputParam>
</HelloWorld>
</soap:Body>
</soap:Envelope>
had the problem. But when I changed my request to SOAP 1.2:
POST /WebService1.asmx HTTP/1.1
Host: www.tempuri.org
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<HelloWorld xmlns="http://www.tempuri.org/">
<inputParam>Привет</inputParam>
</HelloWorld>
</soap12:Body>
</soap12:Envelope>
The issue was solved.
I have a web service with the following contract:
POST /Service/service.asmx HTTP/1.1
Host: xxx.xxx.xxx
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "xxx.xxx.xxx/Service/Method"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<Request xmlns="xxx.xxx.xxx/Service/">
<transactiontype>string</transactiontype>
<username>string</username>
<password>string</password>
</Request>
</soap:Header>
<soap:Body>
<Method xmlns="xxx.xxx.xxx/Service/">
<xml>xml</xml>
</Method>
</soap:Body>
</soap:Envelope>
And I am trying to call the service using jquery. This is my code:
$.ajax({
url: serverUrl + 'Method',
type: "POST",
dataType: "xml",
data: { xml: "xml" },
beforeSend: function (req) {
req.setRequestHeader('Header', '<Request xmlns="xxx.xxx.xxx/Service/">'
+'<transactiontype>4</transactiontype>'
+'<agencyName>name</agencyName>'
+'<username>user</username>'
+'<password>pass</password>'
+'</Request>');
},
success: function (data) {
alert(data.text);
},
error: function (request, status, errorThrown) {
alert(status);
}
});
However, the header content is not passed to the web service? How would I go about passing the header credentials to my web service call?
soap:Header is an XML element inside the XML/SOAP data "payload". This is different than an HTTP headers. In the contract, SOAPAction (along with Content-Length, etc) is an HTTP header.
XmlHttpRequest.setRequestHeader is used for specifying HTTP headers. It has nothing to do with anything inside the XML (directly).
The first answer at Simplest SOAP example should give an example of how to make a SOAP request. Note:
xmlhttp.setRequestHeader("SOAPAction", "http://www.webserviceX.NET/GetQuote");
xmlhttp.setRequestHeader("Content-Type", "text/xml");
...
var xml = '<?xml version="1.0" encoding="utf-8"?>' +
'<soap:Envelope...' + etc;
xmlhttp.send(xml)
It is the XML which contains soap:Envelope and the child elements soap:Header and soap:Body.
Happy coding.