如何从.NET对象创建XML文档? [英] How to create an XML document from a .NET object?

查看:59
本文介绍了如何从.NET对象创建XML文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下接受文件名的变量:

I have the following variable that accepts a file name:

var xtr = new XmlTextReader(xmlFileName) { WhitespaceHandling = WhitespaceHandling.None };
var xd = new XmlDocument();
xd.Load(xtr);

我想对其进行更改,以便可以传递一个对象.我不想先将对象序列化为文件.

I would like to change it so that I can pass in an object. I don't want to have to serialize the object to file first.

这可能吗?

更新:

我最初的意图是获取一个xml文档,合并一些xslt(存储在文件中),然后输出并返回html ...就像这样:

My original intentions were to take an xml document, merge some xslt (stored in a file), then output and return html... like this:

public string TransformXml(string xmlFileName, string xslFileName)
{
     var xtr = new XmlTextReader(xmlFileName) { WhitespaceHandling = WhitespaceHandling.None };
     var xd = new XmlDocument();
     xd.Load(xtr);

     var xslt = new System.Xml.Xsl.XslCompiledTransform();
     xslt.Load(xslFileName);
     var stm = new MemoryStream();
     xslt.Transform(xd, null, stm);
     stm.Position = 1;
     var sr = new StreamReader(stm);
     xtr.Close();
     return sr.ReadToEnd();
}

在上面的代码中,我正在从文件中读取xml.现在,我想做的就是在将对象序列化到文件之前使用它.

In the above code I am reading in the xml from a file. Now what I would like to do is just work with the object, before it was serialized to the file.

所以让我用代码来说明我的问题

So let me illustrate my problem using code

public string TransformXMLFromObject(myObjType myobj , string xsltFileName)
{
     // Notice the xslt stays the same.
     // Its in these next few lines that I can't figure out how to load the xml document (xd) from an object, and not from a file....

     var xtr = new XmlTextReader(xmlFileName) { WhitespaceHandling = WhitespaceHandling.None };
     var xd = new XmlDocument();
     xd.Load(xtr);
}

推荐答案

您想将任意.NET对象转换为序列化的XML字符串吗?没有比这更简单的了!:-)

You want to turn an arbitrary .NET object into a serialized XML string? Nothing simpler than that!! :-)

public string SerializeToXml(object input)
{
   XmlSerializer ser = new XmlSerializer(input.GetType(), "http://schemas.yournamespace.com");
   string result = string.Empty;

   using(MemoryStream memStm = new MemoryStream())
   {
     ser.Serialize(memStm, input);

     memStm.Position = 0;
     result = new StreamReader(memStm).ReadToEnd();
   } 

   return result;
} 

应该做到这一点:-)当然,您可能还希望将默认XML命名空间也配置为参数.

That should to it :-) Of course you might want to make the default XML namespace configurable as a parameter, too.

还是您希望能够在现有对象之上创建XmlDocument?

Or do you want to be able to create an XmlDocument on top of an existing object?

public XmlDocument SerializeToXmlDocument(object input)
{
   XmlSerializer ser = new XmlSerializer(input.GetType(), "http://schemas.yournamespace.com");

   XmlDocument xd = null;

   using(MemoryStream memStm = new MemoryStream())
   {
     ser.Serialize(memStm, input);

     memStm.Position = 0;

     XmlReaderSettings settings = new XmlReaderSettings();
     settings.IgnoreWhitespace = true;

     using(var xtr = XmlReader.Create(memStm, settings))
     {  
        xd = new XmlDocument();
        xd.Load(xtr);
     }
   }

   return xd;
}

这篇关于如何从.NET对象创建XML文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆