XML解析并作为JSON返回

本文关键字:JSON 返回 XML | 更新日期: 2023-09-27 18:13:40

请帮我解析这个文件并获得JSON输出。到目前为止,我能够获得键(名称)和值(值)对。但我不明白如何在这个键值对中获得数组。XML、JSON和c#的新手。大师请帮忙…

<?xml version="1.0" encoding="US-ASCII"?>
<Information>
  <first_name>Frank</first_name>
  <last_name>Murphy</last_name>
  <customer_support_url>google.com</customer_support_url>
  <received_date>12/30/2013</received_date>
  <customer_support_phone_number>(888)111-2222</customer_support_phone_number>
  <employer_name>Google Inc</employer_name>
  <label>Dependent Care</label>
  <set_flag>0</set_flag>
  <employee_id>11111</employee_id>
  <employer_id>111</employer_id>
  <claim_form_id>1234</claim_form_id>
  <employer_url>For more information please go to: &lt;a href=http://google.com&gt;google.com&lt;/a&gt;</employer_url>
  <tax>$1,500.00</tax>
  <claim_form_id>1234</claim_form_id>
  <claim_details>
      <claim_form_id>1234</claim_form_id>
      <amount_claimed>100</amount_claimed>
      <expense_name>ChildCare</expense_name>
      <service_date>2013-06-17</service_date> 
      <claim_status>Approved</claim_status>
      <claim_status_reason/>
  </claim_details>
  <Location>Dublin</Location>

到目前为止,这是我在控制台应用程序中为测试目的所做的:
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.IO;
 using System.Xml;
 using System.Xml.Linq;
 namespace ConsoleApplication1
 {
    class Program
    {
        static void Main(string[] args)
        {
           XDocument doc = XDocument.Load("input.xml");
           // Declare your token Colelction Class 
           foreach (var keyvalue in doc.Root.DescendantNodes().OfType<XElement>().Select(x => new XElement("token", new XElement("name", x.Name), new XElement("value", x.Value))))
           {
               Console.WriteLine(keyvalue);
               //Add name,value to token collection
           }
           // Convert it to json
           //return
           Console.ReadKey();
         }
    }
  }

XML解析并作为JSON返回

可以使用Json。. NET的JsonConvert.SerializeXmlNode()可以轻松地将XML转换为JSON:

XmlDocument doc = new XmlDocument();
doc.Load("input.xml");
string jsonText = JsonConvert.SerializeXmlNode(doc);
结果:

{
   "Information": {
      "first_name": "Frank",
      "last_name": "Murphy",
      "customer_support_url": "google.com",
      "received_date": "12/30/2013",
      "customer_support_phone_number": "(888)111-2222",
      "employer_name": "Google Inc",
      "label": "Dependent Care",
      "set_flag": "0",
      "employee_id": "11111",
      "employer_id": "111",
      "claim_form_id": [
         "1234",
         "1234"
      ],
      "employer_url": "For more information please go to: <a href=http://google.com>google.com<'/a>",
      "tax": "$1,500.00",
      "claim_details": {
         "claim_form_id": "1234",
         "amount_claimed": "100",
         "expense_name": "ChildCare",
         "service_date": "2013-06-17",
         "claim_status": "Approved",
         "claim_status_reason": null
      },
      "Location": "Dublin"
   }
}

首先将XML转换为类,在vs编辑中粘贴特殊的粘贴XML作为类给出此(ps您错过了XML中的结束行)

 /// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
 [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class Information
{
private object[] itemsField;
private ItemsChoiceType[] itemsElementNameField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Location", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("claim_details", typeof(InformationClaim_details))]
[System.Xml.Serialization.XmlElementAttribute("claim_form_id", typeof(ushort))]
[System.Xml.Serialization.XmlElementAttribute("customer_support_phone_number", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("customer_support_url", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("employee_id", typeof(ushort))]
[System.Xml.Serialization.XmlElementAttribute("employer_id", typeof(byte))]
[System.Xml.Serialization.XmlElementAttribute("employer_name", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("employer_url", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("first_name", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("label", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("last_name", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("received_date", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("set_flag", typeof(byte))]
[System.Xml.Serialization.XmlElementAttribute("tax", typeof(string))]
[System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemsElementName")]
public object[] Items
{
    get
    {
        return this.itemsField;
    }
    set
    {
        this.itemsField = value;
    }
  }
  /// <remarks/>
  [System.Xml.Serialization.XmlElementAttribute("ItemsElementName")]
  [System.Xml.Serialization.XmlIgnoreAttribute()]
  public ItemsChoiceType[] ItemsElementName
  {
    get
    {
        return this.itemsElementNameField;
    }
    set
    {
        this.itemsElementNameField = value;
    }
   }
  }
 /// <remarks/>
 [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class InformationClaim_details
{
private ushort claim_form_idField;
private byte amount_claimedField;
private string expense_nameField;
private System.DateTime service_dateField;
private string claim_statusField;
private object claim_status_reasonField;
/// <remarks/>
public ushort claim_form_id
{
    get
    {
        return this.claim_form_idField;
    }
    set
    {
        this.claim_form_idField = value;
    }
}
/// <remarks/>
public byte amount_claimed
{
    get
    {
        return this.amount_claimedField;
    }
    set
    {
        this.amount_claimedField = value;
    }
}
/// <remarks/>
public string expense_name
{
    get
    {
        return this.expense_nameField;
    }
    set
    {
        this.expense_nameField = value;
    }
}
 /// <remarks/>
  [System.Xml.Serialization.XmlElementAttribute(DataType = "date")]
 public System.DateTime service_date
  {
    get
    {
        return this.service_dateField;
    }
    set
    {
        this.service_dateField = value;
    }
}
/// <remarks/>
public string claim_status
{
    get
    {
        return this.claim_statusField;
    }
    set
    {
        this.claim_statusField = value;
    }
  }
  /// <remarks/>
   public object claim_status_reason
  {
    get
    {
        return this.claim_status_reasonField;
    }
    set
    {
        this.claim_status_reasonField = value;
       }
    }
}

然后将XML反序列化为c#对象

XmlSerializer serializer = new XmlSerializer(typeof(Information));

转换为JSonstring Json= JsonConvert.SerializeObject(ObjectInstance);