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

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

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

  • 相关标签:xsd,xml
  • 程序员必备接口测试调试工具:点击使用

    Apipost = Postman + Swagger + Mock + Jmeter

    Api设计、调试、文档、自动化测试工具

    网页生成APP,用做网站的技术去做APP:立即创建

    手机网站开发APP、自助封装APP、200+原生模块、2000+映射JS接口按需打包

    • 上一篇:详解XML命名空间(XML Namespaces)介绍以及节点读取方法的示例代码
    • 下一篇:XML加密和XML签名简介的具体介绍

    相关文章

    相关视频


    • 使用xmlhttp为网站增加域名查询功能的示例代码...
    • 四种XML解析方式详解
    • 基于PHP对XML的操作详解
    • XML和Tomcat的入门知识的详细介绍
    • 详解根据xsd生成xml文档的示例代码分析
    • Vue3 事件修饰符
    • vue3 指令
    • vue3 基础语法
    • vue3 组合api和选项api介绍

    视频教程分类

    • php视频教程
    • html视频教程
    • css视频教程
    • JS视频教程
    • jQuery视频教程
    • mysql视频教程
    • Linux视频教程
    • Python视频教程
    • Laravel视频教程
    • Vue视频教程

    专题

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