根据xsd生成xml文档

寂寞而非孤独

寂寞而非孤独

2016-02-19 20:57

给自己一点时间接受自己,爱自己,趁着下午茶的时间来学习图老师推荐的根据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_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);
              }

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

          }
          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;

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

      }
       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

展开更多 50%)
分享

猜你喜欢

根据xsd生成xml文档

Web开发
根据xsd生成xml文档

根据数据库生成xml二法

电脑网络
根据数据库生成xml二法

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

SQL Server根据查询结果,生成XML文件

SQLServer
SQL Server根据查询结果,生成XML文件

基于PHP与XML的PDF文档生成技术

PHP
基于PHP与XML的PDF文档生成技术

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

如何用ASP生成XML数据文档(RSS订阅)

Web开发
如何用ASP生成XML数据文档(RSS订阅)

使用XML DOM生成XML(3)

Web开发
使用XML DOM生成XML(3)

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

如何在Mac下压缩PDF文件

如何在Mac下压缩PDF文件

windows media player无法播放该文件?

windows media player无法播放该文件?
下拉加载更多内容 ↓