xmlns=''> was not expected. - There is an error in XML document (2, 2)
49,435
Solution 1
Declaring XmlSerializer as
XmlSerializer s = new XmlSerializer(typeof(string),new XmlRootAttribute("response"));
is enough.
Solution 2
You want to deserialize the XML and treat it as a fragment.
There's a very straightforward workaround available here. I've modified it for your scenario:
var webRequest = WebRequest.Create("http://inb374.jelastic.tsukaeru.net:8080/VodafoneDB/webresources/vodafone/04111111");
using (var webResponse = webRequest.GetResponse())
using (var responseStream = webResponse.GetResponseStream())
{
var rootAttribute = new XmlRootAttribute();
rootAttribute.ElementName = "response";
rootAttribute.IsNullable = true;
var xmlSerializer = new XmlSerializer(typeof (string), rootAttribute);
var response = (string) xmlSerializer.Deserialize(responseStream);
}
Solution 3
I had the same error with Deserialize "xml string with 2 namespaces declared" into object.
<?xml version="1.0" encoding="utf-8"?>
<vcs-device:errorNotification xmlns:vcs-pos="http://abc" xmlns:vcs-device="http://def">
<errorText>Can't get PAN</errorText>
</vcs-device:errorNotification>
[XmlRoot(ElementName = "errorNotification", Namespace = "http://def")]
public class ErrorNotification
{
[XmlAttribute(AttributeName = "vcs-pos", Namespace = "http://www.w3.org/2000/xmlns/")]
public string VcsPosNamespace { get; set; }
[XmlAttribute(AttributeName = "vcs-device", Namespace = "http://www.w3.org/2000/xmlns/")]
public string VcsDeviceNamespace { get; set; }
[XmlElement(ElementName = "errorText", Namespace = "")]
public string ErrorText { get; set; }
}
By adding field with [XmlAttribute] into ErrorNotification class deserialization works.
public static T Deserialize<T>(string xml)
{
var serializer = new XmlSerializer(typeof(T));
using (TextReader reader = new StringReader(xml))
{
return (T)serializer.Deserialize(reader);
}
}
var obj = Deserialize<ErrorNotification>(xml);
Related videos on Youtube
Author by
user1384603
Updated on July 09, 2022Comments
-
user1384603 almost 2 years
Im trying to deserialize the response from this simple web service
Im using the following code:
WebRequest request = WebRequest.Create("http://inb374.jelastic.tsukaeru.net:8080/VodafoneDB/webresources/vodafone/04111111"); WebResponse ws = request.GetResponse(); XmlSerializer s = new XmlSerializer(typeof(string)); string reponse = (string)s.Deserialize(ws.GetResponseStream());
-
Trevor Hart over 8 yearsWhat happened with me when I got this error was I was serializing many objects to the same file separately then trying to deserialize said xml file to a list, what fixed it was just deleting the xmlns portion of the xml file, creating a custom list, then serializing only that said list, then I deserialized the xml file to the custom list. I know this has nothing to do with you but I'm going to leave it here because that was the problem I ran into that made me look at this question in the first place so maybe it will help somebody else.
-
-
user1384603 over 11 yearsThanks for your time. I found user L.B answer very simple and useful.
-
Joe over 10 yearsFYI L.B is right, slight variation if there is a Parent node though. In the xml I'm de-serializing the parent node is <PlatformResponse> which was throwing the exception "<Platform xmlns="> was not expected". new XmlRootAttribute("response")); didn't clear the error, new XmlRootAttribute("PlatformResponse")); did. Vote up L.B.
-
Admin about 7 years500 bounty on it's way to you mate. Thanks.
-
Denise Skidmore about 3 yearsAlso note if your root attribute has a namespace on it, that you may need to massage that XmlRootAttribute a little.
xmlRoot.ElementName = reader.Name.Split(':').Last(); xmlRoot.Namespace = reader.NamespaceURI;