Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i get xml file from service , but i have problem when i want
Deserialize 
to object ,, always show me
root element is missing 



xml


XML
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfResultInfo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/">
<ResultInfo>
<PartNumber>KRPA-4007</PartNumber>
<Manufacturer>test</Manufacturer>

</ResultInfo>
</ArrayOfResultInfo>







code

C#
string xml = null;
     WebRequest req = WebRequest.Create(link);
     req.Credentials = CredentialCache.DefaultCredentials;
     WebResponse res = req.GetResponse();
     Stream dataStream = res.GetResponseStream();
     StreamReader reader = new StreamReader(dataStream);
     xml = reader.ReadToEnd();
     XmlSerializer serializer = new XmlSerializer(typeof(ArrayOfResultInfo));
     var result = (ArrayOfResultInfo)serializer.Deserialize(reader);


What I have tried:

string xml = null;
WebRequest req = WebRequest.Create(link);
req.Credentials = CredentialCache.DefaultCredentials;
WebResponse res = req.GetResponse();
Stream dataStream = res.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
xml = reader.ReadToEnd();
XmlSerializer serializer = new XmlSerializer(typeof(ArrayOfResultInfo));
var result = (ArrayOfResultInfo)serializer.Deserialize(reader);
Posted
Updated 20-Feb-17 1:54am
v2
Comments
Graeme_Grant 20-Feb-17 3:10am    
Where are the classes that you are mapping the XML against?
ÂĦmâd Ŝâlâĥ 20-Feb-17 4:05am    
no problem in class i use select nude also 0 result ,,, but i see the problem here

xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/"


but how remove this from reader before Deserialize

This may help you: Convert XML to C# Classes[^]
 
Share this answer
 
Comments
ÂĦmâd Ŝâlâĥ 20-Feb-17 4:05am    
thanks but the problem here


xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/"


but how remove this from reader before Deserialize
Graeme_Grant 20-Feb-17 8:28am    
You don't remove them, it is handled as part of the deserialization process.
Hi,

The following code worked for me with your xml:
C#
string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><ArrayOfResultInfo xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://tempuri.org/\"><ResultInfo><PartNumber>KRPA - 4007 </PartNumber><Manufacturer> test </Manufacturer></ResultInfo></ArrayOfResultInfo>";



           var serializer = new XmlSerializer(typeof(ArrayOfResultInfo));
           ArrayOfResultInfo result;

           using (TextReader reader = new StringReader(xml))
           {
               result = (ArrayOfResultInfo)serializer.Deserialize(reader);
           }

           Console.WriteLine(result.ResultInfo.Manufacturer);
           Console.WriteLine(result.ResultInfo.PartNumber);

           Console.ReadLine();


The classes for xml are as follows:
C#
[XmlRoot(ElementName = "ResultInfo", Namespace = "http://tempuri.org/")]
public class ResultInfo
{
    [XmlElement(ElementName = "PartNumber", Namespace = "http://tempuri.org/")]
    public string PartNumber { get; set; }
    [XmlElement(ElementName = "Manufacturer", Namespace = "http://tempuri.org/")]
    public string Manufacturer { get; set; }
}

[XmlRoot(ElementName = "ArrayOfResultInfo", Namespace = "http://tempuri.org/")]
public class ArrayOfResultInfo
{
    [XmlElement(ElementName = "ResultInfo", Namespace = "http://tempuri.org/")]
    public ResultInfo ResultInfo { get; set; }
    [XmlAttribute(AttributeName = "xsd", Namespace = "http://www.w3.org/2000/xmlns/")]
    public string Xsd { get; set; }
    [XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
    public string Xsi { get; set; }
    [XmlAttribute(AttributeName = "xmlns")]
    public string Xmlns { get; set; }
}


Hope this helps !
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900