栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > 其他 > xml语言

详解根据xsd生成xml文档的示例代码分析

xml语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

详解根据xsd生成xml文档的示例代码分析

现在有很多的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;/// /// ProXML 的摘要说明/// public class ProXml{    public ProXml()    {        //        // TODO: 在此处添加构造函数逻辑        //    }    ///     /// 获得xsd文件路径    ///     public static string GetSchemaPath    {            get{               return HttpContext.Current.Request.PhysicalApplicationPath + "App_Data\system\publish.xsd";           }    }    ///     /// 获理节点    ///     ///     public static System.Collections.Generic.List 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 elements=new System.Collections.Generic.List ();        foreach (XmlSchemaObject obj in schema.Elements.Values)        {            if (obj.GetType() == typeof(XmlSchemaElement))            {                elements.Add((XmlSchemaElement)obj);            }        }        return elements;          }    ///     /// 添加元素    ///     ///     ///     ///     ///     ///     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;        }    }   ///    /// 获得元素   ///    ///    ///     public static System.Collections.Generic.List GetDataItem(string name)    {        System.Collections.Generic.List arr = new System.Collections.Generic.List();        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 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;        }    }    ///     /// 根据节点名获得节点    ///     ///     ///     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;           }       }  ///   /// 校验dom对象  ///   ///   ///        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;       }}

以上就是详解根据xsd生成xml文档的示例代码分析的详细内容,更多请关注考高分网其它相关文章!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/794559.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号