大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要介绍了如何根据xsd生成xml文档,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
成都创新互联公司自2013年起,先为西乌珠穆沁等服务建站,西乌珠穆沁等地企业,进行企业商务咨询服务。为西乌珠穆沁企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
现在有很多的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; } }
感谢你能够认真阅读完这篇文章,希望小编分享的“如何根据xsd生成xml文档”这篇文章对大家有帮助,同时也希望大家多多支持创新互联成都网站设计公司,关注创新互联成都网站设计公司行业资讯频道,更多相关知识等着你来学习!
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、网站设计器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。