сериализация и сохранение объекта в другом объекте, который реализует IXmlSerializable - PullRequest
2 голосов
/ 16 сентября 2011

Я хотел бы XML сериализовать экземпляры моего объекта Исключение и сохранить его в свойстве XMLNode [] Nodes другого объекта ExceptionReport .

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Xml.Serialization.XmlSchemaProviderAttribute("ExportSchema")]
[System.Xml.Serialization.XmlRootAttribute(IsNullable = false)]
public partial class ExceptionReport : object, System.Xml.Serialization.IXmlSerializable
{
    public System.Xml.XmlNode[] Nodes { get; set; }

    public void ReadXml(System.Xml.XmlReader reader)
    {
        this.Nodes = System.Runtime.Serialization.XmlSerializableServices.ReadNodes(reader);
    }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        System.Runtime.Serialization.XmlSerializableServices.WriteNodes(writer, this.Nodes);
    }
}
public class Exception
{
    public string ExceptionText;
    public string exceptionCode;
    public string locator;
}

Как бы я поступил так, чтобы результат был примерно таким:

<ExceptionReport xmlns="http://www.opengis.net/ows" >
  <Exception exceptionCode="1">my first instance</Exception>
  <Exception exceptionCode="2">my second instance</Exception>
</ExceptionReport>

Пока у меня есть следующее, но мне нужно знать, как сериализовать эти объекты и сохранить их в массиве узлов ExceptionReport.

ExceptionReport er = new ExceptionReport();

Exception exception_item1 = new Exception();
exception_item1.ExceptionText = "my first instance";
exception_item1.exceptionCode = "1";

Exception exception_item2 = new Exception();
exception_item2.ExceptionText = "my second instance";
exception_item2.exceptionCode = "2";

List<Exception> exceptions = new List<Exception>( exception_item1, exception_item2 );

1 Ответ

1 голос
/ 11 октября 2011
[XmlRoot("ExceptionReport")]
    public partial class ExceptionReport
    {
        [XmlElement("Exception")]
        public List<Exception> Nodes { get; set; }

        public ExceptionReport()
        {
            Nodes = new List<Exception>();
        }
    }
    public class Exception
    {
        [XmlText]
        public string ExceptionText;
        [XmlAttribute("exceptionCode")]
        public int ExceptionCode;
        [XmlAttribute("locator")]
        public string Locator;
    }

Затем для сериализации я использую следующие расширения:

public static bool XmlSerialize<T>(this T item, string fileName)
        {
            return item.XmlSerialize(fileName, true);
        }
        public static bool XmlSerialize<T>(this T item, string fileName, bool removeNamespaces)
        {
            object locker = new object();

            XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();
            xmlns.Add(string.Empty, string.Empty);

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.OmitXmlDeclaration = true;

            lock (locker)
            {
                using (XmlWriter writer = XmlWriter.Create(fileName, settings))
                {
                    if (removeNamespaces)
                    {
                        xmlSerializer.Serialize(writer, item, xmlns);
                    }
                    else { xmlSerializer.Serialize(writer, item); }

                    writer.Close();
                }
            }

            return true;
        }
        public static T XmlDeserialize<T>(this string s)
        {
            object locker = new object();
            StringReader stringReader = new StringReader(s);
            XmlTextReader reader = new XmlTextReader(stringReader);
            try
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
                lock (locker)
                {
                    T item = (T)xmlSerializer.Deserialize(reader);
                    reader.Close();
                    return item;
                }
            }
            finally
            {
                if (reader != null)
                { reader.Close(); }
            }
        }
        public static T XmlDeserialize<T>(this FileInfo fileInfo)
        {
            string xml = string.Empty;
            using (FileStream fs = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read))
            {
                using (StreamReader sr = new StreamReader(fs))
                {
                    return sr.ReadToEnd().XmlDeserialize<T>();
                }
            }
        }

Используйте вот так:

ExceptionReport report = new ExceptionReport();
            report.Nodes.Add(new Exception { ExceptionText = "my first instance", ExceptionCode = 1, Locator = "loc1" });
            report.Nodes.Add(new Exception { ExceptionText = "my second instance", ExceptionCode = 2 });
            report.XmlSerialize("C:\\test.xml");

Я тестировал, и получилось так, как вы хотели.Надеюсь, это поможет ...

PS - Расширения пришли из моей библиотеки на codeproject: http://www.codeproject.com/KB/dotnet/MBGExtensionsLibrary.aspx

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...