C# XML字符串轉DataTable

今天總結一下 關於XML字符串轉DataTable 方法:spa

 

引用:using System.Xml;code

using Newtonsoft.Json;
using System.Data;xml

using System.Collections;blog

首先,定義一個xml字符串來接收傳過來的數據,字符串

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

 ESBRequest request = XMLUntils.Deserialize(typeof(ESBRequest), xml) as ESBRequest;//將xml 反序列化爲  ESBRequest類string

 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();io

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

 request.RequestBody = PublicFunction.RemoveAttribute(root);stream

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];
        }
相關文章
相關標籤/搜索