设为首页
加入收藏
站内地图
旧版入口
当前位置:首页 > 站长学院 > 网络编程 > ASP.NET

根据xsd生成xml文档

作者:佚名 出处:网络转载 时间:11-02 点击:

内容载入中...

现在有很多的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);
                    }

                }
                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>
    /// 根据节点名获得节点
    /// </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;
       }
}
http://www.cnblogs.com/eric812/archive/2006/11/01/546914.html

收藏本文:
】【打印页面】【推荐给朋友】【关闭窗口

站长学院

推荐信息