搜索
首页后端开发XML/RSS教程详解根据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;


/// <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
RSS提要:探索XML的作用和目的RSS提要:探索XML的作用和目的Apr 28, 2025 am 12:06 AM

XML在RSSFeed中的作用是结构化数据、标准化和提供可扩展性。1.XML使得RSSFeed的数据结构化,便于解析和处理。2.XML提供了一种标准化的方式来定义RSSFeed的格式。3.XML的可扩展性使得RSSFeed可以根据需要添加新的标签和属性。

缩放XML/RSS处理:性能优化技术缩放XML/RSS处理:性能优化技术Apr 27, 2025 am 12:28 AM

处理XML和RSS数据时,可以通过以下步骤优化性能:1)使用高效的解析器如lxml提升解析速度;2)采用SAX解析器减少内存使用;3)利用XPath表达式提高数据提取效率;4)实施多进程并行处理提升处理速度。

RSS文档格式:探索RSS 2.0及以后RSS文档格式:探索RSS 2.0及以后Apr 26, 2025 am 12:22 AM

RSS2.0是一种开放标准,允许内容发布者以结构化的方式分发内容。它包含了丰富的元数据,如标题、链接、描述、发布日期等,使得订阅者能够快速浏览和访问内容。RSS2.0的优势在于其简洁和扩展性。例如,它允许自定义元素,这意味着开发者可以根据需求添加额外的信息,如作者、分类等。

理解RSS:XML观点理解RSS:XML观点Apr 25, 2025 am 12:14 AM

RSS是一种基于XML的格式,用于发布经常更新的内容。1.RSSfeed通过XML结构化组织信息,包括标题、链接、描述等。2.创建RSSfeed需按照XML结构编写,添加元数据如语言和发布日期。3.高级用法可包含多媒体文件和分类信息。4.调试时使用XML验证工具,确保必需元素存在且编码正确。5.优化RSSfeed可通过分页、缓存和保持结构简洁来实现。通过理解和应用这些知识,可以有效管理和分发内容。

XML中的RSS:解码标签,属性和结构XML中的RSS:解码标签,属性和结构Apr 24, 2025 am 12:09 AM

RSS是一种基于XML的格式,用于发布和订阅内容。RSS文件的XML结构包括根元素、元素和多个元素,每个代表一个内容条目。通过XML解析器读取和解析RSS文件,用户可以订阅并获取最新内容。

XML在RSS中的优势:技术深度潜水XML在RSS中的优势:技术深度潜水Apr 23, 2025 am 12:02 AM

XML在RSS中具有结构化数据、可扩展性、跨平台兼容性和解析验证的优势。1)结构化数据确保内容的一致性和可靠性;2)可扩展性允许添加自定义标签以适应内容需求;3)跨平台兼容性使其在不同设备上无缝工作;4)解析和验证工具确保Feed的质量和完整性。

XML中的RSS:揭示内容联合的核心XML中的RSS:揭示内容联合的核心Apr 22, 2025 am 12:08 AM

RSS在XML中的实现方式是通过结构化的XML格式来组织内容。1)RSS使用XML作为数据交换格式,包含频道信息和项目列表等元素。2)生成RSS文件需按规范组织内容,发布到服务器供订阅。3)RSS文件可通过阅读器或插件订阅,实现内容自动更新。

超越基础:高级RSS文档功能超越基础:高级RSS文档功能Apr 21, 2025 am 12:03 AM

RSS的高级功能包括内容命名空间、扩展模块和条件订阅。1)内容命名空间扩展RSS功能,2)扩展模块如DublinCore或iTunes添加元数据,3)条件订阅根据特定条件筛选条目。这些功能通过添加XML元素和属性实现,提升信息获取效率。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器