|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
那做企业软件是不是最好用J2EE?asp.net|xml|技能|天生xml如今有良多的xml工具软件都能依据xsd文件誊写出xml文档,.net没有完成此办法,如是我写了几个扫瞄、校验、和创立xml的办法
全体代码以下:
usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
usingSystem.Xml;
usingSystem.Xml.Schema;
usingSystem.Collections;
/**////<summary>
///ProXML的择要申明
///</summary>
publicclassProXml
{
publicProXml()
{
//
//TODO:在此处增加机关函数逻辑
//
}
/**////<summary>
///取得xsd文件路径
///</summary>
publicstaticstringGetSchemaPath
{
get{
returnHttpContext.Current.Request.PhysicalApplicationPath+"App_Datasystempublish.xsd";
}
}
/**////<summary>
///获理节点
///</summary>
///<returns></returns>
publicstaticSystem.Collections.Generic.List<XmlSchemaElement>GetDatas()
{
XmlSchemaSetxsSet=newXmlSchemaSet();
xsSet.Add("http://www.w3.org/2001/XMLSchema",GetSchemaPath);
xsSet.Compile();
XmlSchemaschema=null;
foreach(XmlSchemaxsinxsSet.Schemas())
{
schema=xs;
}
System.Collections.Generic.List<XmlSchemaElement>elements=newSystem.Collections.Generic.List<XmlSchemaElement>();
foreach(XmlSchemaObjectobjinschema.Elements.Values)
{
if(obj.GetType()==typeof(XmlSchemaElement))
{
elements.Add((XmlSchemaElement)obj);
}
}
returnelements;
}
/**////<summary>
///增加元素
///</summary>
///<paramname="sourceXsd"></param>
///<paramname="titles"></param>
///<paramname="sourceXml"></param>
///<paramname="sourceNd"></param>
///<paramname="values"></param>
publicstaticvoidAddElement(XmlSchemaObjectsourceXsd,Hashtabletitles,XmlDocumentsourceXml,XmlNodesourceNd,string[]values)
{
if(sourceXsd.GetType()==typeof(XmlSchemaChoice))
{
XmlSchemaChoicechoice=sourceXsdasXmlSchemaChoice;
decimalmin=choice.MinOccurs;
foreach(XmlSchemaObjectiteminchoice.Items)
{
if(item.GetType()==typeof(XmlSchemaElement))
{
stringname=((XmlSchemaElement)item).Name;
if(titles.ContainsKey(name))
{
XmlElementelement=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);
}
}
}
elseif(sourceXsd.GetType()==typeof(XmlSchemaElement))
{
stringname=((XmlSchemaElement)sourceXsd).Name;
if(titles.ContainsKey(name))
{
XmlElementelement=sourceXml.CreateElement(name);
element.InnerText=values[(int)titles[name]];
sourceNd.AppendChild(element);
}
}
elseif(sourceXsd.GetType()==typeof(XmlSchemaSequence))
{
foreach(XmlSchemaObjectchildItemin((XmlSchemaSequence)sourceXsd).Items)
{
if(childItem.GetType()==typeof(XmlSchemaElement))
{
stringname=((XmlSchemaElement)childItem).Name;
if(titles.ContainsKey(name))
{
XmlElementelement=sourceXml.CreateElement(name);
element.InnerText=values[(int)titles[name]];
sourceNd.AppendChild(element);
}
}
else
{
AddElement(childItem,titles,sourceXml,sourceNd,values);
}
}
}
else
{
return;
}
}
/**////<summary>
///取得元素
///</summary>
///<paramname="name"></param>
///<returns></returns>
publicstaticSystem.Collections.Generic.List<XmlSchemaElement>GetDataItem(stringname)
{
System.Collections.Generic.List<XmlSchemaElement>arr=newSystem.Collections.Generic.List<XmlSchemaElement>();
XmlSchemaElementelement=GetTableSchema(name);
if(element==null)
{
returnnull;
}
XmlSchemaComplexTypecomplex=element.SchemaTypeasXmlSchemaComplexType;
XmlSchemaSequencesequence=complex.ContentTypeParticleasXmlSchemaSequence;
foreach(XmlSchemaObjectobjinsequence.Items)
{
if(obj.GetType()==typeof(XmlSchemaElement))
{
XmlSchemaElementitem=(XmlSchemaElement)obj;
arr.Add(item);
}
else
{
GetItem(arr,obj);
}
}
returnarr;
}
publicstaticvoidGetItem(System.Collections.Generic.List<XmlSchemaElement>arr,XmlSchemaObjectobj)
{
if(obj.GetType()==typeof(XmlSchemaElement))
{
XmlSchemaElementitem=(XmlSchemaElement)obj;
arr.Add(item);
}
elseif(obj.GetType()==typeof(XmlSchemaChoice))
{
XmlSchemaChoicechoice=objasXmlSchemaChoice;
foreach(XmlSchemaObjectchildinchoice.Items)
{
if(child.GetType()==typeof(XmlSchemaElement))
{
XmlSchemaElementitem=childasXmlSchemaElement;
arr.Add(item);
}
else
{
GetItem(arr,child);
}
}
}
elseif(obj.GetType()==typeof(XmlSchemaSequence))
{
XmlSchemaSequencesequence=objasXmlSchemaSequence;
foreach(XmlSchemaObjectchildinsequence.Items)
{
if(child.GetType()==typeof(XmlSchemaObject))
{
XmlSchemaElementitem=childasXmlSchemaElement;
arr.Add(item);
}
else
{
GetItem(arr,child);
}
}
}
else
{
return;
}
}
/**////<summary>
///依据节点名取得节点
///</summary>
///<paramname="name"></param>
///<returns></returns>
publicstaticXmlSchemaElementGetTableSchema(stringname)
{
XmlSchemaSetxsSet=newXmlSchemaSet();
xsSet.Add("http://www.w3.org/2001/XMLSchema",GetSchemaPath);
xsSet.Compile();
XmlSchemaschema=null;
foreach(XmlSchemaxsinxsSet.Schemas())
{
schema=xs;
}
XmlQualifiedNameqf=newXmlQualifiedName(name,"http://www.w3.org/2001/XMLSchema");
if(schema.Elements.Contains(qf))
{
return(XmlSchemaElement)schema.Elements[qf];
}
returnnull;
}
staticvoidXmlValidation(objectsendor,ValidationEventArgse)
{
switch(e.Severity)
{
caseXmlSeverityType.Error:
throwe.Exception;
caseXmlSeverityType.Warning:
throwe.Exception;
}
}
/**////<summary>
///校验dom工具
///</summary>
///<paramname="doc"></param>
///<returns></returns>
publicstaticstringCheckDataXml(XmlDocumentdoc)
{
XmlSchemaSetxsd=newXmlSchemaSet();
xsd.Add("",GetSchemaPath);
doc.Schemas=xsd;
try
{
doc.Validate(newValidationEventHandler(XmlValidation));
}
catch(Exceptionex)
{
returnex.Message;
}
returnnull;
}
}
你所列的那些其实差不多都可以称为应用服务器(servlet应该说是一种语言更合适)java是开放的,相同的工具就会有很多公司在做,加上java已经发展了很多年了,因此这些工具就很多了。他们很多都是类似的。 |
|