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; }
    
           }
    }
  • 相关阅读:
    intel Skylake平台安装WIN7
    复杂表单
    sqlalchemy的cascades
    新版mysql 5.7的group_by非常不和谐
    sqlalchemy使用tip
    sql分组数据去重
    flask-sqlalchemy relationship
    老笔记本装xubuntu+win7
    [leetcode]Reorder List
    [leetcode]Insertion Sort List
  • 原文地址:https://www.cnblogs.com/zix1314/p/5639108.html
Copyright © 2011-2022 走看看