我可以从c#类Object创建xmlreader对象吗?订单对象 [英] Can i create the xmlreader object from c# class Object e.g. Order Object
问题描述
大家好,
我正在尝试借助c#对象创建和xml.
现在我有一个订单对象,并希望借助XslCompiledTransform来通过在其上应用xsl来创建一个xml文件.
但是XslCompiledTransform将参数作为字符串输入uri或xml阅读器
所以我的问题是我可以通过传递我的orderObject来创建xmlreader对象,以便可以通过将其传递给XslCompiledTransform来进一步使用xmlreader来创建xml文件.
我的订单对象
Hello all,
I am trying to create and xml with the help of c# object.
right now i am having one order object and with the help of XslCompiledTransform i want to create a xml file by applying xsl on it.
But XslCompiledTransform take the parameter as string input uri or xml reader
so my question is can i create the xmlreader object by passing my orderObject so that i can further use the xmlreader for creating the xml file by passing it to XslCompiledTransform.
My Order object
public class OrderObject
{
public string OrderName { get; set; }
public string Price { get; set; }
}
然后单击按钮,我正在做类似
的事情
and on button click i am doing some thing like
XmlWriterSettings writerSettings = new XmlWriterSettings(); //Object of XmlWriterSettings to generate the xml output.
writerSettings.Encoding = Encoding.GetEncoding("ISO-8859-1");
XsltArgumentList xsltArgumentList = new XsltArgumentList();
XmlWriter writer = null;
try
{
writer = XmlWriter.Create(_outputPath, writerSettings); //Start Creating the xml document
#if DEBUG
XslCompiledTransform transform = new XslCompiledTransform(true);
#else
XslCompiledTransform transform = new XslCompiledTransform();
#endif
XsltSettings xsltSettings = new XsltSettings(); //Create object of XsltSettings and set its EnableScript = true
xsltSettings.EnableScript = true;
transform.Load(_xslFilePath, xsltSettings, new XmlUrlResolver()); //Load master.xml with xsltSettings
List<OrderObject> newOrderData = OrderData.NewOrder;
XmlReader xmlReader = XmlReader.Create(); //????
transform.Transform(xmlReader, xsltArgumentList, writer); //Apply xsl and output xml
writer.Flush(); //flush the writer buffer
writer.Close(); //close the writer
lblMessage.Text = "XML Output file is generated successfully Path:" + _outputPath;
} //try
catch (Exception ex)
{
writer.Close(); //if error occur then close the writer
lblMessage.Text = "Error occurred while generating the xml!" + ex.Message;
} //catch
在代码的以下行,我被卡住了
At the below line in code i m getting stuck-up
List<OrderObject> newOrderData = OrderData.NewOrder;
XmlReader xmlReader = XmlReader.Create(); //????</pre>
有什么方法可以使用orderobjectlist创建xml
或还有其他方法可以处理订单对象吗?
在此先感谢
Is there any way that i can use the orderobjectlist for creating the xml
or Is there any other way i can work through the order object
Thanks in advance
推荐答案
已解决:
下面的方法ObjectToXml将obj和filename作为参数,并在XmlSerializer的帮助下在给定路径上创建xml文件.
Solved:
the below method ObjectToXml take the obj and filename as the parameters and with the help of XmlSerializer it create the xml file on the given path
namespace Utility
{
public class Serial
{
public static void ObjectToXml(object obj, string path_to_xml)
{
//serialize and persist it to it's file
try
{
if(File.Exists(path_to_xml))
File.Delete(path_to_xml);
XmlSerializer ser = new XmlSerializer(obj.GetType(),new XmlRootAttribute("document"));
FileStream fs = File.Open(
path_to_xml, // File Path
FileMode.OpenOrCreate, // File Mode
FileAccess.Write, // File Access
FileShare.ReadWrite); // File Share
ser.Serialize(fs, obj);
fs.Close(); // Close the file
}
catch (Exception ex)
{
throw new Exception("Could Not Serialize the object to " + path_to_xml, ex);
}
}
}
}
这篇关于我可以从c#类Object创建xmlreader对象吗?订单对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!