程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> 根據xsd生成xml文檔

根據xsd生成xml文檔

編輯:.NET實例教程

現在有很多的xml工具軟件都能根據xsd文件書寫出XML文檔,.Net 沒有實現此方法,如是我寫了幾個浏覽、校驗、和創建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: 在此處添加構造函數邏輯
        //
    }
    /**//// <summary>
    /// 獲得xsd文件路徑
    /// </summary>
    public static string GetSchemaPath
    {
           get{
               return HttpContext.Current.Request.PhysicalApplicationPath + "App_Data\\system\\publish.xsd";
           }
    }
    /**//// <summary>
    /// 獲理節點
    /// </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);
                    }

         &nbsp;      }
                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))
        {
      &nbsp;     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>
    /// 根據節點名獲得節點
    /// </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>
  /// 校驗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;
       }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved