0

I want to covert to string into object with value. I mean let's say i have string that has XML code inside like:

Code Snippet:

String response =@"<?xml version=""1.0"" encoding=""utf-8""?>\r\n
<Request>\r\n  
<TransactionType>ADMIN</TransactionType>\r\n   
<Username>abc</Username>\r\n  
<Password>def</Password>\r\n  
</Request>";

I have a Class that has all the properties which mentioned in Xml like

Class ResponseClass

String UserName;

String Password;

String Transaction;

How can I set all the values in ResponseClass object without string parsing? I have tried it with serialization but it gives me some problem in windows 8.1 app store project due to limitation in API.

Is there any way to get it sorted?

Thanks

Mario Stoilov
  • 3,411
  • 5
  • 31
  • 51
user3064724
  • 125
  • 1
  • 9
  • 1
    Any of these should help: [Deserializing XML from String](http://stackoverflow.com/questions/14645964/deserializing-xml-from-string), [C# - Convert XML String to Object](http://stackoverflow.com/questions/3187444/c-sharp-convert-xml-string-to-object), and [XML string deserialization into c# object](http://stackoverflow.com/questions/14722492/xml-string-deserialization-into-c-sharp-object). If not, you should be able to use [XElement.Parse](http://msdn.microsoft.com/en-us/library/bb468714(v=vs.110).aspx) an traverse it with LINQ. – valverij Jan 13 '14 at 15:34
  • I'm curious about the problem you're facing using XmlSerialization - shouldn't this be support for Windows 8.1 App Store Projects? – Filburt Jan 13 '14 at 17:06

3 Answers3

1

Here's a very simple way using XDocument.Parse(String) from System.Xml.Linq:

String response = @"<?xml version=""1.0"" encoding=""utf-8""?>
    <Request> 
        <TransactionType>ADMIN</TransactionType>   
        <Username>abc</Username> 
        <Password>def</Password> 
    </Request>";

var xml = XDocument.Parse(response);
var request = xml.Element("Request");

var responseObject = new ResponseClass()
{
    UserName = request.Element("Username").Value,
    Password = request.Element("Password").Value,
    Transaction = request.Element("TransactionType").Value,
};

Or, if the Windows store apps support it, you can use the built in XmlSerializer (if not, you can just ignore this bit). Just define your class using the XmlRoot and XmlElement attributes like this:

[XmlRoot("Request")]
public class ResponseClass
{
    [XmlElement("Username")]
    public String UserName { get; set; }

    [XmlElement("Password")]
    public String Password { get; set; }

    [XmlElement("TransactionType")]
    public String Transaction { get; set; }
}

and then use the create an XmlSerializer and StringReader to deserialize it:

String response = @"<?xml version=""1.0"" encoding=""utf-8""?>
    <Request> 
        <TransactionType>ADMIN</TransactionType>   
        <Username>abc</Username> 
        <Password>def</Password> 
    </Request>";

var serializer = new XmlSerializer(typeof(ResponseClass));
ResponseClass responseObject;

using (var reader = new StringReader(response))
{
    responseObject = serializer.Deserialize(reader) as ResponseClass;
}
valverij
  • 4,871
  • 1
  • 22
  • 35
0

Unless the Xml was generated via some serializer you will have to code it manually to match. Serialization and deserialization go hand in hand. Therefore if your xml file is the product of a serializer then there would most likely be a deserializer. If you have the option of re-creating this xml file then do so using an XmlSerializer, that way you can deserialize.

T McKeown
  • 12,971
  • 1
  • 25
  • 32
0

If you don't want to use XmlSerializer thenThis is bit ugly but hope this will help with what you required. I have implemented something like that for some of my use. First make an extension method.

public static class TextUtil
{
     public static string JustAfter(this string Str, string Seq, string SeqEnd)
     {
        string Orgi = Str;
        try
        {
            int i = Str.IndexOf(Seq);

            if (i < 0)
                return null;

            i = i + Seq.Length;

            int j = Str.IndexOf(SeqEnd, i);
            int end;

            if (j > 0) end = j - i;
            else end = Str.Length - i;

            return Orgi.Substring(i, end);
        }
        catch (Exception)
        {
            return String.Empty;
        }
    }
}

Now you can use it as shown.

private void ParseXml(string responce) // respnce is xml string.
{
    string transactionType = responce.JustAfter("<TransactionType>", "</TransactionType>");
    string userName = responce.JustAfter("<Username>", "</UserName>");
    string password = responce.JustAfter("<Password>", "</Password>");

    ResponceClass resClass = new RespnceClass()
    {
        Transaction = transactionType,
        UserName = userName,
        Password = password
    });
}
Muhammad Umar
  • 3,761
  • 1
  • 24
  • 36