All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
C#闪烁问题解决方法_.net资料_编程技术
delphi动作列表组件(TActionList)使用实例
Asp.Net,Forms验证(自定义、角色提供程序、单点登录)_[Asp.Net教程]
PHP基础:PHP4和PHP5的配置异同比较_php资料_编程技术
利用PHP上传文件_[PHP教程]
用Ajax和RSS制作一个首页新闻_[AJAX教程]
在ASP.NET中实现多文件上传和insertAdjacentHTML_[Asp.Net教程]
C#之委托和事件_[Asp.Net教程]
SQLServer2000启动1069错误_[SQL,Server教程]
Delphi中管理sql server索引
Reading number is top 10 pictures
Female model behind the bitterness, often being overcharged2
西班牙山村小景5
Sora aoi after swimming
海底兵马俑
Ashlynn Brooke photograph of a group3
Chinese paper-cut grilles art appreciation3
Sora aoi mirror memorial classics5
Chinese paper-cut grilles art appreciation8
A man's favorite things4
Beauty ZhiHuiLin1
Download software ranking
I'm come from Beijing2
传奇私服架设教程-chm
Unix video tutorial14
功夫熊猫2(下集)
Unix video tutorial1
Unix video tutorial18
仙剑奇侠传98版歌曲
Unix video tutorial4
Boxer's Top ten classic battle8
金山office2007
aaa published in(发表于) 2013/12/11 8:29:17 Edit(编辑)
ASP.NET技巧:根据xsd生成xml文档_.net资料_编程技术

ASP.NET技巧:根据xsd生成xml文档_.net资料_编程技术

ASP.NET技巧:根据xsd生成xml文档_.net资料_编程技术-你的首页-uuhomepage.com

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






添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.