zoukankan      html  css  js  c++  java
  • XML参数转换为Object,并转换为List或DataTable

    demo效果:

    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Runtime.Serialization.Json;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Xml;
    
    namespace Json转对象
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                var varEntity = ToObject(@"<Response>
                            <ResultCode>1</ResultCode>
                            <ResultMsg></ResultMsg>
                            <Item>
                            <DeptID>2001</DeptID>
                            <DeptName>神经内门诊</DeptName>
                            </Item>
                            <Item>
                            <DeptID>2021</DeptID>
                            <DeptName>胸外门诊</DeptName>
                            </Item>
                            </Response>", false);
    
                List<testEntity> deptList = new List<testEntity>();
                DataTable dt = new DataTable();
                dt.Columns.Add("DeptID"); 
                dt.Columns.Add("DeptName");
                var objectList = varEntity.Item;
                foreach (var entityItem in objectList)
                {
                    testEntity deptEntity = new testEntity();
                    deptEntity.DeptID = entityItem.DeptID;
                    deptEntity.DeptName = entityItem.DeptName;
    
                    deptList.Add(deptEntity);
    
                    DataRow dr = dt.NewRow();
                    dr["DeptID"] = entityItem.DeptID;
                    dr["DeptName"] = entityItem.DeptName;
    
                    dt.Rows.Add(dr);
                }
                dataGridView1.DataSource = deptList;
                dataGridView2.DataSource = dt;
            }
    
            /// <summary>
            /// XML参数转换为Object
            /// </summary>
            /// <param name="IsAddHead">是否添加xml头</param>
            /// <returns></returns>
            public static dynamic ToObject(string xml, bool IsAddHead) 
            {
                string head = @"<?xml version=""1.0"" encoding=""utf-8"" ?> ";
                XmlDocument doc = new XmlDocument();
                if (IsAddHead)
                {
                    doc.LoadXml(head + xml);
                }
                else
                {
                    doc.LoadXml(xml);
                }
                string Json = JsonConvert.SerializeXmlNode(doc);
                dynamic obj = (JsonConvert.DeserializeObject(Json) as dynamic)["Response"];
                return obj;
            }
        }
     
        public class testEntity
        {    
            public string DeptID { get; set; }
        
            public string DeptName { get; set; }
    
           }
    }
  • 相关阅读:
    PL/SQL注册码
    分页sql
    js获取url值
    C语言中的bool类型 stdbool.h
    语音朗读小程序
    50. Pow(x, n)
    二维数组旋转
    用一位数组代替二维数组作为形参使用
    单链表排序——交换数据成员
    C++重载输入流、输出流运算符
  • 原文地址:https://www.cnblogs.com/zix1314/p/5639108.html
Copyright © 2011-2022 走看看