0

My project is Asp.net Core
I want to convert XML data to C# class,XML data has a node PersonelInfo I try to read XML but It's code not working. How can I solve this code? What is my problem?

var xmlGetDetailsUser = new XmlDocument();
xmlGetDetailsUser.LoadXml(await responseMessageGetDetailsUser.Content.ReadAsStringAsync());
using (StringReader reader = new StringReader(xmlGetDetailsUser.InnerXml))
{
    try
    {
        PersonelInfo data = (PersonelInfo)(serializer.Deserialize(reader));
    }
    catch (System.Exception e)
    { }
}

class

public class PersonelInfo
{
  public string PersonelCode { get; set; }
  public string Email { get; set; }
}

xmlGetDetailsUser.InnerXml has this value :

<?xml version=\"1.0\" encoding=\"utf-8\"?>
<ArrayOfPersonelInfo xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"sample url .....\">
    <PersonelInfo>
        <PersonelCode>99999</PersonelCode>    
        <Email>test@test.com</Email>        
    </PersonelInfo>
</ArrayOfPersonelInfo>

when run my program show this exception in try catch

There is an error in XML

Mohammad Daliri
  • 1,370
  • 6
  • 20
  • 43
  • 1
    Possible duplicate of [Convert XML String to Object](https://stackoverflow.com/questions/3187444/convert-xml-string-to-object) – Feras Al Sous Aug 19 '18 at 07:20
  • Could the escaped `"` characters be the cause? – Sebastian Hofmann Aug 19 '18 at 07:21
  • It would be very odd for the *inner* XML to actually contain an ` xml ... ?>` declaration. Could you post the complete XML? – Jon Skeet Aug 19 '18 at 07:21
  • @DaisyShipton It is complete XML – Mohammad Daliri Aug 19 '18 at 07:41
  • @SebastianHofmann What do you mean about **escaped " characters**? – Mohammad Daliri Aug 19 '18 at 07:43
  • @FerasAlSous My question is different it because of my problem XML Data. This XML Data has a node the name of **PersonelInfo** and I don't know How can I get this node? – Mohammad Daliri Aug 19 '18 at 07:50
  • @MohammadDaliri: Look at the XML you've shown - it's got a `\` before each of the `"` characters. Either you've copied that from the debugger (so that's not the *actual* XML) or it's invalid XML. – Jon Skeet Aug 19 '18 at 08:46
  • It seems that you don't actually write any kind of message inside your catch block. What is the real message you get? (An empty try/catch block in this context is useless, better remove it and let the exception (and the InnerException) show their messages) – Steve Aug 19 '18 at 09:50

2 Answers2

4

You should set namespace for XmlSerializer and change type of it to List<PersonelInfo>.

Try this:

XmlSerializer serializer = new XmlSerializer(typeof(List<PersonelInfo>), "sample url ....");
            XmlReaderSettings settings = new XmlReaderSettings();
            using (StringReader textReader = new StringReader(await responseMessageGetDetailsUser.Content.ReadAsStringAsync()))
            {
                using (XmlReader xmlReader = XmlReader.Create(textReader, settings))
                {
                    var data = (List<PersonelInfo>)serializer.Deserialize(xmlReader);
                }
            }
Hossein
  • 3,083
  • 3
  • 16
  • 33
0

I Found the answers but It seems is not a good way for convert XML to C# What do you think of improving this code?

var xmlGetDetailsUser = new XmlDocument();
xmlGetDetailsUser.LoadXml(await responseMessageGetDetailsUser.Content.ReadAsStringAsync());
var result = xmlGetDetailsUser.GetElementsByTagName("PersonelInfo");
XmlSerializer serializer = new XmlSerializer(typeof(PersonelInfo));
var personelInfo = new PersonelInfo();
personelInfo.PersonelCode = result.Item(0)["PersonelCode"].InnerText;
personelInfo.Email = result.Item(0)["Email"].InnerText;
Mohammad Daliri
  • 1,370
  • 6
  • 20
  • 43
  • Could you share us the method defination for `web service`? Will it return an array or an object? Per to xml, it seems to be array instead of object. Try something like`responseMessageGetDetailsUser.Content.ReadAsAsync()` and `responseMessageGetDetailsUser.Content.ReadAsAsync>()` – Edward Aug 20 '18 at 04:29
  • @TaoZhou My Webservice return array of class – Mohammad Daliri Oct 02 '18 at 14:05