I got an XML response as:
<?xml version='1.0' encoding='UTF-8'?>
<tsResponse xmlns="site.com/api" xmlns:xsi="site.org/2001/XMLSchema-instance" xsi:schemaLocation="site.com/api help.site.com/samples/en-us/rest_api/ts-api_3_4.xsd">
<credentials token="xxxxxx-xxxxxx-xxxxxx">
<site id="xxxxxx-xxxxxx-xxxxxx" contentUrl="sitename"/>
<user id="xxxxxx-xxxxxx-xxxxxx"/>
</credentials>
</tsResponse>
How can I parse this response to get the token value inside credentials?
CodePudding user response:
If you put the XML string that you get into the variable xmlToParse, you can do:
TsResponse result = _xmlParser.Deserialize<TsResponse>(xmlToParse);
Where _xmlParser
is an instance of the class:
using System.Xml.Serialization;
namespace yourNameSpace
{
public class XmlParser
{
public T Deserialize<T>(string input) where T : class
{
XmlSerializer ser = new XmlSerializer(typeof(T));
using (StringReader sr = new StringReader(input))
{
return (T)ser.Deserialize(sr);
}
}
}
}
You also need to model the whole XML object as a class in C# (XmlObjects.Credentials in my example) using the for example the page https://json2csharp.com/code-converters/xml-to-csharp which gives you:
// using System.Xml.Serialization;
// XmlSerializer serializer = new XmlSerializer(typeof(TsResponse));
// using (StringReader reader = new StringReader(xml))
// {
// var test = (TsResponse)serializer.Deserialize(reader);
// }
[XmlRoot(ElementName="site")]
public class Site {
[XmlAttribute(AttributeName="id")]
public string Id { get; set; }
[XmlAttribute(AttributeName="contentUrl")]
public string ContentUrl { get; set; }
}
[XmlRoot(ElementName="user")]
public class User {
[XmlAttribute(AttributeName="id")]
public string Id { get; set; }
}
[XmlRoot(ElementName="credentials")]
public class Credentials {
[XmlElement(ElementName="site")]
public Site Site { get; set; }
[XmlElement(ElementName="user")]
public User User { get; set; }
[XmlAttribute(AttributeName="token")]
public string Token { get; set; }
}
[XmlRoot(ElementName="tsResponse")]
public class TsResponse {
[XmlElement(ElementName="credentials")]
public Credentials Credentials { get; set; }
[XmlAttribute(AttributeName="xmlns")]
public string Xmlns { get; set; }
[XmlAttribute(AttributeName="xsi")]
public string Xsi { get; set; }
[XmlAttribute(AttributeName="schemaLocation")]
public string SchemaLocation { get; set; }
}
, then you can go with
result.Credentials.Token
CodePudding user response:
By using LINQ to XML.
c#
void Main()
{
XDocument xdoc = XDocument.Parse(@"<tsResponse xmlns='site.com/api' xmlns:xsi='site.org/2001/XMLSchema-instance' xsi:schemaLocation='site.com/api help.site.com/samples/en-us/rest_api/ts-api_3_4.xsd'>
<credentials token='xxxxxx-xxxxxx-xxxxxx'>
<site id='xxxxxx-xxxxxx-xxxxxx' contentUrl='sitename'/>
<user id='xxxxxx-xxxxxx-xxxxxx'/>
</credentials>
</tsResponse>");
XNamespace ns1 = xdoc.Root.GetDefaultNamespace();
string token = xdoc.Descendants(ns1 "credentials")
.FirstOrDefault()?.Attribute("token").Value;
Console.WriteLine("token = {0}", token);
}
Output
token = xxxxxx-xxxxxx-xxxxxx
CodePudding user response:
I think that the simplest way is to use LINQ to XML's XDocument:
using System.Xml;
using System.Xml.Linq;
/* ... */
var xml = @"<?xml version='1.0' encoding='UTF-8'?>
<tsResponse xmlns=""site.com/api"" xmlns:xsi=""site.org/2001/XMLSchema-instance"" xsi:schemaLocation=""site.com/api help.site.com/samples/en-us/rest_api/ts-api_3_4.xsd"">
<credentials token=""xxxxxx-xxxxxx-xxxxxx"">
<site id=""xxxxxx-xxxxxx-xxxxxx"" contentUrl=""sitename""/>
<user id=""xxxxxx-xxxxxx-xxxxxx""/>
</credentials>
</tsResponse>";
var xmlns = XNamespace.Get("site.com/api");
var token = XDocument.Parse(xml)
.Element(xmlns "tsResponse")?
.Element(xmlns "credentials")?
.Attribute("token")?
.Value;