开发者

Best way to create an XML document and save to a file

I have an object that I want to create an XML from, what's the best way to do this in ASP.NET 3.5?

I want to save it to a file also (web application).

I don't want to serialize the object, s开发者_运维知识库ince I will have some custom element names and attributes and values that will be modified during XML creation.


It is a bit hard to answer without knowing how "custom" is "custom", but LINQ-to-XML is a good bet since you have .NET 3.5:

// using variables here to show how the names etc can be decided at runtime
string elName = "Fred";
DateTime when = DateTime.Today;
int id = 123;
 string idName = "id";
var el = new XElement(elName, new XAttribute(idName, id), when);
el.Save("out.xml");

Giving the xml:

<Fred id=\"123\">2010-03-01T00:00:00+00:00</Fred>

If the file is huge, then XmlWriter may be more efficient, but the code is harder.


If you don't want to serialize you could use XmlWriter.


You should use System.Xml.Linq, which is the easiest way to manipulate XML by far.


Since you will have dynamic document, I'd suggest System.Xml.Linq.XDocument to generate it. Then you just use XDocument.Save to save it.


You can use the System.Xml namespace.

using System.Xml;

XmlDocument document = new XmlDocument();

XmlNode rootNode = document.CreateElement("root");
document.AppendChild(rootNode);

// Create other nodes related to your object and append them to the root node.

document.Save("path/to/your/xml/file.xml");


I used XmlDocument before I learned about serialization.


Why not implement the ISerializable interface? Then you have full control over the serialization process yourself...

See examples here: http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iserializable.aspx and here: http://msdn.microsoft.com/en-us/library/ms182342(VS.80).aspx

You can combine this with XmlSerializer, if you have a complex object you need to serialize, an you don't want to write all of the serialization yourself.

If it's just part of the object (i.e. some properties) you want to do special serialization of, you can make these objects that implements ISerializable, and change the value to whatever you need it to be, during serialization.


Consider creating a helper class that uses XML serialization. That decouples the XML formatting from the main class's design, and generally speaking is in keeping with the idea of adding functionality to classes through composition. (In fact, it's often a good idea to do it this way even if the helper class doesn't use XML serialization.) It also lets you format the XML declaratively without getting having to write a lot of procedural code.

How to do this: design the helper class so that it exposes public properties in a fashion that XmlSerializer likes, and gets those values from an instance of your class that's passed in to a private constructor. Add a static XmlSerializer property, and a public static method that uses the XmlSerializer to write the data to a file or a stream or whatever. Something like:

[XmlRoot("MyClass")]
public class MyClassXmlWriter
{
   private static XmlSerializer Serializer = new XmlSerializer(typeof MyClassXmlWriter);

   public static void Write(MyClass source, Stream st)
   {
      Serializer.Serialize(new MyClassXmlWriter(source), st);
   }

   private MyClass Source;

   private MyClassXmlWriter(MyClass source)
   {
      Source = source;
   }

   [XmlElement("SomeElement")]
   public string SomeProperty { get { return Source.SomeProperty; } }

}

Using this is as simple as:

using (FileStream fs = new FileStream(filename))
{
   MyClassXmlWriter.Write(myObject, fs);
}

Note that if you ever need to implement deserialization, you just give its properties public getters and then implement a static Read method that deserializes into a new MyClassXmlWriter object and then creates and populates a MyClass object from its properties. (You'd probably change the name of the class, though.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜