Convert from string to object in a Windows 8.1 app app C #

I want to hide a string in an object with a value. I mean, let's say that I have a string that has XML code inside, for example:

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 mentioned in Xml, like

Class ResponseClass

String UserName;

String Password;

String Transaction;

How to set all values ​​in a ResponseClass object without parsing strings ? I tried it with serialization, but this gives me some problem in the Windows 8.1 app store project due to limitations in the API.

Is there any way to sort it?

thanks

0
source share
3 answers

XDocument.Parse(String) 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,
};

, Windows , XmlSerializer ( , ). XmlRoot XmlElement, :

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

create XmlSerializer StringReader :

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

Xml , . . , XML , , . xml , XmlSerializer, .

0

XmlSerializer, , , , . - . .

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

, .

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

Source: https://habr.com/ru/post/1523492/


All Articles