根据xsd生成xml文档

2016-02-19 20:57 109 1 收藏

给自己一点时间接受自己,爱自己,趁着下午茶的时间来学习图老师推荐的根据xsd生成xml文档,过去的都会过去,迎接崭新的开始,释放更美好的自己。

【 tulaoshi.com - Web开发 】

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

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/webkaifa/)

  
  /**//// summary
  /// ProXML 的摘要说明
  /// /summary
  public class ProXml
  {
      public ProXml()
      {
          //
          // TODO: 在此处添加构造函数逻辑
          //
      }
      /**//// summary
      /// 获得xsd文件路径
      /// /summary
      public static string GetSchemaPath
      {
             get{
                 return HttpContext.Current.Request.PhysicalApplicationPath + "App_Datasystempublish.xsd";
             }
      }
      /**//// summary
      /// 获理节点
      /// /summary
      /// returns/returns
      public static System.Collections.Generic.ListXmlSchemaElement 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.ListXmlSchemaElement elements=new System.Collections.Generic.ListXmlSchemaElement ();
          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.ListXmlSchemaElement GetDataItem(string name)
      {
          System.Collections.Generic.ListXmlSchemaElement arr = new System.Collections.Generic.ListXmlSchemaElement();
          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.ListXmlSchemaElement 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;

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/webkaifa/)

  
             }

         }
    /**//// 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

来源:http://www.tulaoshi.com/n/20160219/1624907.html

延伸阅读
标签: Web开发
Javascript文档生成是比较头疼的问题,不过现在已经有开源的工具可以使用——JsDoc。 其官方站点地址:http://jsdoc.sourceforge.net/ Release版的下载地址是:http://sourceforge.net/projects/jsdoc JsDoc是根据Javascript代码中的注释生成相应的帮助文档,它定义了一套注释标签规范。因此在编写Javascript代码时,要根据...
标签: Web开发
一,必须弄清楚最终需要的是什么 我们通过ASP或其他动态编程语言,最终需要的是XML格式的数据,这点和XML数据所在的文件载体无关,它可以是实实在在的XML文件,比如:http://www.dw8.cn/common/dw8.xml 。也可以为asp文档,比如:http://www.cnbruce.com/blog/rss2.asp 他们都是XML数据的体现,为了实现XML数据的动态,所以需要使用到动态编...
标签: Web开发
项目兼容需要生成一系列的XML文件,总结了下XML文件的生成基本方式 XmlTextWriter w = new XmlTextWriter("C:XML文件名.xml", Encoding.Unicode); //Encoding.Unicode为生成XML文件的编码格式,到时候合输出:?xml version="1.0" encoding="utf-16"? w.Formatting = Formatting.Indented; // 这个比较重要,这个属性说明xml文件里面的内容是...
标签: Web开发
有两文件: objXML.asp:测试文件 clsXML.asp:vbs类文件 代码: objXML.asp %@ Language=VBScript % % Option Explicit % !--#INCLUDE FILE="clsXML.asp"-- % Dim objXML, strPath, str Set objXML = New clsXML strPath = Server.MapPath(".") & "New.xml" objXML.createFile strPath, "Root" 'Or If using an existing XML file: &#...
标签: Web开发
XML文档因为其固有的描述性特性而趋向于变得很罗嗦。其结果是文档会由于被描述的数据增多而变得很长,而这种很大的文档会在需要同其他实体进行交换时出现问题。和其他文档(比如普通文本文件(flat file)或者Electronic Data Interchange (EDI))比起来XML文档就显得特别冗长。为了举例说明这个概念,让我们看看以下这个普通文本文件: John...

经验教程

405

收藏

15
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部