VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > c#编程 >
  • C# XML字符串转DataTable

今天总结一下 关于XML字符串转DataTable 方法:

 

引用:using System.Xml;

using Newtonsoft.Json;
using System.Data;

using System.Collections;

首先,定义一个xml字符串来接收传过来的数据,

 string xml = context.Request["xml"]; //<ESBRequest><RequestHeader><ServiceID>35.A01.06.202</ServiceID> </RequestHeader><RequestBody><Rows>...<Row> </Row><Row>... </Row> </Rows></RequestBody></ESBRequest>

 ESBRequest request = XMLUntils.Deserialize(typeof(ESBRequest), xml) as ESBRequest;//将xml 反序列化为  ESBRequest类

复制代码
 public static object Deserialize(Type type, string xml)
        {
            try
            {
                using (StringReader sr = new StringReader(xml))
                {
                    XmlSerializer xmldes = new XmlSerializer(type);
                    return xmldes.Deserialize(sr);
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }
复制代码
复制代码
// 注意: 生成的代码可能至少需要 .NET Framework 4.5 或 .NET Core/Standard 2.0。
    [System.SerializableAttribute]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public partial class ESBRequest
    {
        private ESBEntryAccessControl accessControlField;

        private ESBEntryRequestHeader RequestHeaderField;

        private string messageBodyField;

        /// <summary>
        /// AccessControl
        /// </summary>
        public ESBEntryAccessControl AccessControl
        {
            get
            {
                return accessControlField;
            }

            set
            {
                accessControlField = value;
            }
        }

        /// <summary>
        /// RequestHeader
        /// </summary>
        public ESBEntryRequestHeader RequestHeader
        {
            get
            {
                return RequestHeaderField;
            }

            set
            {
                RequestHeaderField = value;
            }
        }

        /// <summary>
        /// RequestBody
        /// </summary>
        [System.Xml.Serialization.XmlTextAttribute]
        public string RequestBody
        {
            get
            {
                return messageBodyField;
            }

            set
            {
                messageBodyField = value;
            }
        }
    }
复制代码

 XmlDocument doc = new XmlDocument();

doc.LoadXml(xml);
XmlNode root = doc.FirstChild;

 request.RequestBody = PublicFunction.RemoveAttribute(root);

System.Data.DataTable dsInterNet = ConvertXMLToDt(request.RequestBody);

复制代码
  public static DataTable ConvertXMLToDt(string xmlData)
        {
            StringReader stream = null;
            XmlTextReader reader = null;

            DataSet xmlDS = new DataSet();
            stream = new StringReader(xmlData);
            //从stream装载到XmlTextReader
            reader = new XmlTextReader(stream);
            xmlDS.ReadXml(reader);
            reader.Close();
            return xmlDS.Tables[0];
        }
复制代码

 


相关教程