xsd生成xml文档
请问如何利用C#根据xsd生成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是一个标准的二维表(不存在表套表的情况)可以通过所谓的"类型化DataSet"非常简单的实现.VS中提供了操作的类可以实现,但要是后一种情况就很麻烦了.只能通过XPATH定位去添加了.至少我是这么做
--------------------编程问答-------------------- 说实在的浪客兄弟容易把人的头弄大.实际操作起来我觉得远比这简单 --------------------编程问答-------------------- hi wangchao1982老兄.
你做的由xsd生成xml的例子能发一下吗?
我的邮箱是xulei_88@126.com
十分感谢... --------------------编程问答-------------------- ao\ --------------------编程问答-------------------- hi zhangliu_521
能介绍一下你这个代码怎么使用吗?
谢谢谢谢!!!! --------------------编程问答-------------------- 能说明一下吗??/
ggg
补充:.NET技术 , C#