求救:如何用xsd去生成一个xml文档?

gabriel1 2012-02-29 03:19:05
不了解怎么操作xsd,现在要求自动用xsd生成示例xml,不能用xmlspy等工具,要代码实现。怎么做?

网上找到一串代码,可是不知道怎么用。

悲剧。。



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>
///
namespace XMLTools
{
public class ProXml
{
public ProXml()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/**/
/// <summary>
/// 获得xsd文件路径
/// </summary>
public static string GetSchemaPath
{
get
{
return "common.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;
}
}
}
...全文
232 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
jshi123 2012-02-29
  • 打赏
  • 举报
回复
zhangdaowu5 2012-02-29
  • 打赏
  • 举报
回复
XMLSpyEnt 使用工具
nonocast 2012-02-29
  • 打赏
  • 举报
回复
都转成list,然后根据要求自己用XmlDocument生成啊
gabriel1 2012-02-29
  • 打赏
  • 举报
回复
恩,我知道xsd.exe可以生成类,然后序列化成xml,但是要求不能用工具实现 -_-
种草德鲁伊 2012-02-29
  • 打赏
  • 举报
回复
.net framework sdk里带了一个xsd.exe,用它可以生成,也是.net程序,用reflector啥的可以直接看源代码
gabriel1 2012-02-29
  • 打赏
  • 举报
回复
非常感谢楼上的代码。

可是它没有告诉我如何用xsd生成xml文档啊。
nonocast 2012-02-29
  • 打赏
  • 举报
回复
gabriel1 2012-02-29
  • 打赏
  • 举报
回复
木有一个人懂?

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧