开发者

Export a collection of objects (which in turn contains a collection object) to xml

开发者 https://www.devze.com 2023-02-20 15:50 出处:网络
I have a data object class which stores several properties. One is folder, and another is a string[] of all of the files in that array.

I have a data object class which stores several properties. One is folder, and another is a string[] of all of the files in that array.

What I need to do i开发者_如何学Gos write this out to xml, like follows:

<X>
   <a>dir</a>
      <b>file</f>

So all of the files (there is a string[] array per data object), is nested below the directory field.

Is this easily possible? Or is there an external library which can do this easily for me?

Thanks


you mean something like this:

var myxml = new XElement(yourObj.FolderName);
myxml.Add(new XElement("Files",yourObj.Files.Select(x => new XElement("File",x)));


Use Xml Serializer to do the work for you?

using System.Linq;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.IO;
using System;

namespace NS
{
    public class Data
    {
        public class Nested
        {
            public string The     { get; set; }
            public string[] stuff = {"lazy Cow Jumped Over", "The", "Moon"};
        }

        public List<Nested> Items;
    }

    static class Helper
    {
        public static string ToXml<T>(this T obj) where T:class, new()
        {
            if (null==obj) return null;

            using (var mem = new MemoryStream())
            {
                var ser = new XmlSerializer(typeof(T));
                ser.Serialize(mem, obj);
                return System.Text.Encoding.UTF8.GetString(mem.ToArray());
            }
        }

        public static T FromXml<T>(this string xml) where T: new()
        {
            using (var mem = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(xml)))
            {
                var ser = new XmlSerializer(typeof(T));
                return (T) ser.Deserialize(mem);
            }
        }

    }

    class Program
    {
        public static void Main(string[] args)
        {
            var data = new Data { Items = new List<Data.Nested> { new Data.Nested {The="1"} } };
            Console.WriteLine(data.ToXml());

            var clone = data.ToXml().FromXml<Data>();
            Console.WriteLine("Deserialized: {0}", !ReferenceEquals(data, clone));
            Console.WriteLine("Identical: {0}", Equals(data.ToXml(), clone.ToXml()));
        }
    }
}

Will output

<?xml version="1.0" encoding="utf-8"?>
<Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Items>
    <Nested>
      <stuff>
        <string>lazy Cow Jumped Over</string>
        <string>The</string>
        <string>Moon</string>
      </stuff>
      <The>1</The>
    </Nested>
  </Items>
</Data>
Deserialized: True
Identical: True

There are some cornercases and gotchas especially when working with existing XSD, but this is all very well-trod and documented elsewhere.

0

精彩评论

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

关注公众号