C# 如何将MessageEnvelope xmlns转换为json

C# 如何将MessageEnvelope xmlns转换为json,c#,json,xml,C#,Json,Xml,我已经集成了内部API,响应是XML格式的,我希望它是JSON格式的。我只想将返回的API结果转换为JSON,然后进行映射 如何将XML转换为JSON并进行映射 HttpClient client = new HttpClient(); const string BaseUrl = "myUrl"; client.BaseAddress = new Uri(BaseUrl); cl

我已经集成了内部API,响应是XML格式的,我希望它是JSON格式的。我只想将返回的API结果转换为JSON,然后进行映射

如何将XML转换为JSON并进行映射

            HttpClient client = new HttpClient();
            const string BaseUrl = "myUrl";
            client.BaseAddress = new Uri(BaseUrl);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
            var byteArray = Encoding.ASCII.GetBytes(username + ":" + password);
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));

            HttpResponseMessage odsResponse = await client.GetAsync("api method i am consuming");

            //trying to convert xml to json
            var xx = JsonConvert.SerializeObject(odsResponse, Newtonsoft.Json.Formatting.Indented);

            //this null is returning null
            Viewmodel.AccountLiteExt acc = JsonConvert.DeserializeObject<Viewmodel.AccountLiteExt>(xx);

            if (odsResponse.IsSuccessStatusCode)
            {
                xx = odsResponse.Content.ReadAsStringAsync().Result;
                Console.WriteLine(xx);
                Console.ReadLine();
            }
将其转换为C#类以进行数据映射

    public class AccountLiteExt    {
    public string Id { get; set; } 
    public string Status { get; set; } 
    public string Type { get; set; } 
    public string CSN { get; set; } 
    public string Location { get; set; } 
    public string MasterAccountId { get; set; } 
    public string Name { get; set; } 
}

public class AccountLite    {
    public List<AccountLiteExt> AccountLiteExt { get; set; } 
    public string _xmlns { get; set; } 
}

public class MessageEnvelope    {
    public AccountLite AccountLite { get; set; } 
    public string _xmlns { get; set; } 
}

public class Root    {
    public MessageEnvelope MessageEnvelope { get; set; } 
}
公共类accountlitext{
公共字符串Id{get;set;}
公共字符串状态{get;set;}
公共字符串类型{get;set;}
公共字符串CSN{get;set;}
公共字符串位置{get;set;}
公共字符串MasterAccountId{get;set;}
公共字符串名称{get;set;}
}
公共类AccountLite{
公共列表AccountLiteText{get;set;}
公共字符串_xmlns{get;set;}
}
公共类消息信封{
公共AccountLite AccountLite{get;set;}
公共字符串_xmlns{get;set;}
}
公共类根{
公共消息信封{get;set;}
}

通过首先创建XmlDocument实例找到了解决方案,之后,我必须转换为JSON,然后序列化XMLNode和反序列化Object

            var xml = result.Content.ReadAsStringAsync().Result;
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            string json = JsonConvert.SerializeXmlNode(doc);
            Model model = JsonConvert.DeserializeObject<Model>(json);
var xml=result.Content.ReadAsStringAsync().result;
XmlDocument doc=新的XmlDocument();
doc.LoadXml(xml);
字符串json=JsonConvert.SerializeXmlNode(doc);
Model Model=JsonConvert.DeserializeObject(json);

以前已经有人问过了,您可以在这里查看。
            var xml = result.Content.ReadAsStringAsync().Result;
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            string json = JsonConvert.SerializeXmlNode(doc);
            Model model = JsonConvert.DeserializeObject<Model>(json);