问题
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
回答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;
}
回答2:
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.
回答3:
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
});
}
来源:https://stackoverflow.com/questions/21095082/conversion-from-string-to-object-in-windows-8-1-store-app-c-sharp