C#JSON与XML转换

本文介绍了如何在C#中进行JSON到XML以及XML到JSON的转换。提供了具体的代码示例,展示了如何使用自定义方法GetCustomItemSpecifics进行JSON到XML的转换,以及如何使用XmlToJSONHelper.cs中的方法将XML转换为JSON。
摘要由CSDN通过智能技术生成

C#JSON转XML

输入:[{\'name\': \'yancy\',\'value\': \'0\'},{\'name\': \'jieny\',\'value\': \'1\'}]

string str = "[{\'name\': \'yancy\',\'value\': \'0\'},{\'name\': \'jieny\',\'value\': \'1\'}]";

调用方法:GetCustomItemSpecifics(str)

 

输出:<?xml version="1.0" encoding="utf-8"?><CustomItemSpecifics xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><ItemSpecifics><NameValueListType><Name xmlns="urn:ebay:apis:eBLBaseComponents">yancy</Name><Value xmlns="urn:ebay:apis:eBLBaseComponents">0</Value></NameValueListType><NameValueListType><Name xmlns="urn:ebay:apis:eBLBaseComponents">jieny</Name><Value xmlns="urn:ebay:apis:eBLBaseComponents">1</Value></NameValueListType></ItemSpecifics></CustomItemSpecifics>

 代码:

GetCustomItemSpecifics

public static string GetCustomItemSpecifics(string str)
        {
            DataTable dt= JsonConvert.DeserializeObject<DataTable>(str);

            List<Json2Xml.NameValueListType> nvl = new List<Json2Xml.NameValueListType>();

            foreach (DataRow dr in dt.Rows)
            {
                Json2Xml.NameValueListType nv = new Json2Xml.NameValueListType();

                string sName = dr["Name"].ToString();
                string sValue = dr["Value"].ToString();

                if (sName != string.Empty && sValue != string.Empty)
                {
                    nv.Name = sName;
                    nv.Value = new string[] { sValue };
                    nvl.Add(nv);
                }
            }

            if (nvl.Count == 0)
            {
                return string.Empty;
            }
            else
            {
                Json2Xml.CustomItemSpecifics t = new Json2Xml.CustomItemSpecifics();
                t.ItemSpecifics = nvl.ToArray();
                return Json2Xml.ObjectToText(t, typeof(Json2Xml.CustomItemSpecifics));
            }
        }

Json2Xml.cs

using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApp1
{
    public class Json2Xml
    {
        [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.7.2612.0")]
        [System.SerializableAttribute()]
        [System.Xml.Serialization.XmlTypeAttribute(Namespace = "urn:ebay:apis:eBLBaseComponents")]
        public enum ItemSpecificSourceCodeType
        {

            /// <remarks/>
            ItemSpecific,

            /// <remarks/>
            Attribute,

            /// <remarks/>
            Product,

            /// <remarks/>
            CustomCode,
        }

        [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.7.2612.0")]
        [System.SerializableAttribute()]
        [System.Diagnostics.DebuggerStepThroughAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值