简体   繁体   中英

Conversion from string to object in Windows 8.1 store app c#

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

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;
}

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.

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
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM