溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何根據(jù)xsd生成xml文檔

發(fā)布時(shí)間:2021-07-27 11:01:03 來源:億速云 閱讀:245 作者:小新 欄目:編程語言

這篇文章主要介紹了如何根據(jù)xsd生成xml文檔,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

現(xiàn)在有很多的xml工具軟件都能根據(jù)xsd文件書寫出xml文檔,.net 沒有實(shí)現(xiàn)此方法,如是我寫了幾個(gè)瀏覽、校驗(yàn)、和創(chuàng)建xml的方法
全部代碼如下:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Xml.Schema;
using System.Collections;


/// <summary>
/// ProXML 的摘要說明
/// </summary>
public class ProXml
{
    public ProXml()
    {
        //
        // TODO: 在此處添加構(gòu)造函數(shù)邏輯
        //
    }
    /// <summary>
    /// 獲得xsd文件路徑
    /// </summary>
    public static string GetSchemaPath
    { 
           get{
               return HttpContext.Current.Request.PhysicalApplicationPath + "App_Data\\system\\publish.xsd";
           }
    }
    /// <summary>
    /// 獲理節(jié)點(diǎn)
    /// </summary>
    /// <returns></returns>
    public static System.Collections.Generic.List<XmlSchemaElement> GetDatas()
    {
        XmlSchemaSet xsSet = new XmlSchemaSet();
        xsSet.Add("http://www.w3.org/2001/XMLSchema", GetSchemaPath);
        xsSet.Compile();
        XmlSchema schema = null;
        foreach (XmlSchema xs in xsSet.Schemas())
        {
            schema = xs;
        }
        System.Collections.Generic.List<XmlSchemaElement> elements=new System.Collections.Generic.List<XmlSchemaElement> ();
        foreach (XmlSchemaObject obj in schema.Elements.Values)
        {
            if (obj.GetType() == typeof(XmlSchemaElement))
            {
                elements.Add((XmlSchemaElement)obj);
            }

        }
        return elements;
      
    }
    /// <summary>
    /// 添加元素
    /// </summary>
    /// <param name="sourceXsd"></param>
    /// <param name="titles"></param>
    /// <param name="sourceXml"></param>
    /// <param name="sourceNd"></param>
    /// <param name="values"></param>
    public static   void AddElement(XmlSchemaObject sourceXsd, Hashtable titles, XmlDocument sourceXml, XmlNode sourceNd, string[] values)
    {

        if (sourceXsd.GetType() == typeof(XmlSchemaChoice))
        {
            XmlSchemaChoice choice = sourceXsd as XmlSchemaChoice;
            decimal min = choice.MinOccurs;
            foreach (XmlSchemaObject item in choice.Items)
            {
                if (item.GetType() == typeof(XmlSchemaElement))
                {
                    string name = ((XmlSchemaElement)item).Name;
                    if (titles.ContainsKey(name))
                    {
                        XmlElement element = sourceXml.CreateElement(name);
                       // element.InnerText = ((Excel.Range)st.Cells[rowIndex, (int)titles[name]]).Value2.ToString();
                        element.InnerText = values[(int)titles[name]];
                        sourceNd.AppendChild(element);
                    }

                }
                else
                {
                    AddElement (item, titles, sourceXml, sourceNd, values);
                }

            }


        }
        else if (sourceXsd.GetType() == typeof(XmlSchemaElement))
        {
            string name = ((XmlSchemaElement)sourceXsd).Name;
            if (titles.ContainsKey(name))
            {
                XmlElement element = sourceXml.CreateElement(name);
                element.InnerText = values[(int)titles[name]];
                sourceNd.AppendChild(element);
            }

        }
        else if (sourceXsd.GetType() == typeof(XmlSchemaSequence))
        {
            foreach (XmlSchemaObject childItem in ((XmlSchemaSequence)sourceXsd).Items)
            {
                if (childItem.GetType() == typeof(XmlSchemaElement))
                {
                    string name = ((XmlSchemaElement)childItem).Name;
                    if (titles.ContainsKey(name))
                    {
                        XmlElement element = sourceXml.CreateElement(name);
                        element.InnerText = values[(int)titles[name]];
                        sourceNd.AppendChild(element);
                    }
                }
                else
                {
                    AddElement(childItem, titles, sourceXml, sourceNd, values);
                }

            }
        }
        else
        {
            return;
        }
    }
   /// <summary>
   /// 獲得元素
   /// </summary>
   /// <param name="name"></param>
   /// <returns></returns>
    public static System.Collections.Generic.List<XmlSchemaElement> GetDataItem(string name)
    {
        System.Collections.Generic.List<XmlSchemaElement> arr = new System.Collections.Generic.List<XmlSchemaElement>();
        XmlSchemaElement element = GetTableSchema(name);
        if (element == null)
        {
            return null;
        }
        XmlSchemaComplexType complex = element.SchemaType as XmlSchemaComplexType;
        XmlSchemaSequence sequence = complex.ContentTypeParticle as XmlSchemaSequence;
     
        foreach (XmlSchemaObject obj in sequence.Items)
        {
            if (obj.GetType() == typeof(XmlSchemaElement))
            {
                XmlSchemaElement item = (XmlSchemaElement)obj;
                arr.Add(item);
               
            }
            else
            {
                GetItem(arr, obj);
            }
        }
        return arr;
    }
    public static void GetItem(System.Collections.Generic.List<XmlSchemaElement> arr, XmlSchemaObject obj)
    {
        if (obj.GetType() == typeof(XmlSchemaElement))
        {
            XmlSchemaElement item = (XmlSchemaElement)obj;
            arr.Add(item);
        }
        else if (obj.GetType() == typeof(XmlSchemaChoice))
        {
            XmlSchemaChoice choice = obj as XmlSchemaChoice;
            foreach (XmlSchemaObject child in choice.Items)
            {
                if (child.GetType() == typeof(XmlSchemaElement))
                {
                    XmlSchemaElement item = child as XmlSchemaElement;
                    arr.Add(item);
                }
                else
                {
                    GetItem(arr, child);
                }
            }
        }
        else if (obj.GetType() == typeof(XmlSchemaSequence))
        {
            XmlSchemaSequence sequence = obj as XmlSchemaSequence;
            foreach (XmlSchemaObject child in sequence.Items)
            {
                if (child.GetType() == typeof(XmlSchemaObject))
                {
                    XmlSchemaElement item = child as XmlSchemaElement;
                    arr.Add(item);
                }
                else
                {
                    GetItem(arr, child);
                }
            }
        }
        else
        {
            return;
        }
    }
    /// <summary>
    /// 根據(jù)節(jié)點(diǎn)名獲得節(jié)點(diǎn)
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public static XmlSchemaElement GetTableSchema(string name)
    {
        XmlSchemaSet xsSet = new XmlSchemaSet();
        xsSet.Add("http://www.w3.org/2001/XMLSchema", GetSchemaPath);
        xsSet.Compile();
        XmlSchema schema=null;
        foreach (XmlSchema xs in xsSet.Schemas())
        {
            schema = xs;
        }
        XmlQualifiedName qf = new XmlQualifiedName(name, "http://www.w3.org/2001/XMLSchema");
        if(schema.Elements.Contains(qf))
        {
            return (XmlSchemaElement)schema.Elements[qf];
        }
        return null;

    }
     static  void XmlValidation(object sendor, ValidationEventArgs e)
       {
           switch (e.Severity)
           {
               case XmlSeverityType.Error:
                   throw e.Exception;

               case XmlSeverityType.Warning:
                   throw e.Exception;


           }

       }
  /// <summary>
  /// 校驗(yàn)dom對象
  /// </summary>
  /// <param name="doc"></param>
  /// <returns></returns>
       public static string CheckDataXml(XmlDocument doc)
       {
           XmlSchemaSet xsd = new XmlSchemaSet();
           xsd.Add("", GetSchemaPath);
           doc.Schemas = xsd;
           try
           {
               doc.Validate(new ValidationEventHandler(XmlValidation));
           }
           catch (Exception ex)
           {
               return ex.Message;
           }
           return null;
       }
}

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“如何根據(jù)xsd生成xml文檔”這篇文章對大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI