我有这个 XML
<?xml version="1.0" encoding="utf-8"?>
<office:document-meta xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:presentation="urn:oasis:names:tc:opendocument:xmlns:presentation:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:smil="urn:oasis:names:tc:opendocument:xmlns:smil-compatible:1.0" xmlns:anim="urn:oasis:names:tc:opendocument:xmlns:animation:1.0" xmlns:grddl="http://www.w3.org/2003/g/data-view#" xmlns:officeooo="http://openoffice.org/2009/office" office:version="1.2" grddl:transformation="http://docs.oasis-open.org/office/1.2/xslt/odf2rdf.xsl">
<office:meta>
<meta:creation-date>2014-09-16T11:15:44.96</meta:creation-date>
<meta:editing-duration>PT00H01M36S</meta:editing-duration>
<meta:editing-cycles>2</meta:editing-cycles>
<dc:date>2014-09-16T11:17:09.59</dc:date>
<meta:document-statistic meta:object-count="24" />
<meta:generator>OpenOffice.org/3.2$Win32 OpenOffice.org_project/320m12$Build-9483</meta:generator>
</office:meta>
</office:document-meta>
现在我想反序列化 XML。
我得到了元信息,如创建日期、日期和生成器,但对象计数是 NULL 而不是 24,并且 doc.version 也是 NULL,而它应该是 1.2。
我究竟做错了什么 ?
OpenOffice.ODP.Meta.DocumentMeta mydoc = Tools.XML.Serialization.DeserializeXmlFromFile<OpenOffice.ODP.Meta.DocumentMeta>(@"D:\UserName\Tests\Presentation_1.odp\meta.xml");
string ver = mydoc.Version;
Console.WriteLine(mydoc);
Console.WriteLine(ver);
序列化类:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
namespace Tools.XML
{
// http://www.switchonthecode.com/tutorials/csharp-tutorial-xml-serialization
// http://www.codeproject.com/KB/XML/xml_serializationasp.aspx
public class Serialization
{
public static void SerializeToXml<T>(T ThisTypeInstance, string strFileNameAndPath)
{
SerializeToXml<T>(ThisTypeInstance, new System.IO.StreamWriter(strFileNameAndPath));
} // End Sub SerializeToXml
public static string SerializeToXml<T>(T ThisTypeInstance)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
string strReturnValue = null;
SerializeToXml<T>(ThisTypeInstance, new System.IO.StringWriter(sb));
strReturnValue = sb.ToString();
sb = null;
return strReturnValue;
} // End Function SerializeToXml
public static void SerializeToXml<T>(T ThisTypeInstance, System.IO.TextWriter tw)
{
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
using (System.IO.TextWriter twTextWriter = tw)
{
serializer.Serialize(twTextWriter, ThisTypeInstance);
twTextWriter.Close();
} // End Using twTextWriter
serializer = null;
} // End Sub SerializeToXml
public static T DeserializeXmlFromFile<T>(string strFileNameAndPath)
{
T tReturnValue = default(T);
using (System.IO.FileStream fstrm = new System.IO.FileStream(strFileNameAndPath, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
tReturnValue = DeserializeXmlFromStream<T>(fstrm);
fstrm.Close();
} // End Using fstrm
return tReturnValue;
} // End Function DeserializeXmlFromFile
public static T DeserializeXmlFromEmbeddedRessource<T>(string strRessourceName)
{
T tReturnValue = default(T);
System.Reflection.Assembly ass = System.Reflection.Assembly.GetExecutingAssembly();
using (System.IO.Stream fstrm = ass.GetManifestResourceStream(strRessourceName))
{
tReturnValue = DeserializeXmlFromStream<T>(fstrm);
fstrm.Close();
} // End Using fstrm
return tReturnValue;
} // End Function DeserializeXmlFromEmbeddedRessource
public static T DeserializeXmlFromStream<T>(System.IO.Stream strm)
{
System.Xml.Serialization.XmlSerializer deserializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
T ThisType = default(T);
using (System.IO.StreamReader srEncodingReader = new System.IO.StreamReader(strm, System.Text.Encoding.UTF8))
{
ThisType = (T)deserializer.Deserialize(srEncodingReader);
srEncodingReader.Close();
} // End Using srEncodingReader
deserializer = null;
return ThisType;
} // End Function DeserializeXmlFromStream
#if notneeded
public static void SerializeToXML<T>(System.Collections.Generic.List<T> ThisTypeInstance, string strConfigFileNameAndPath)
{
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(System.Collections.Generic.List<T>));
using (System.IO.TextWriter textWriter = new System.IO.StreamWriter(strConfigFileNameAndPath)) {
serializer.Serialize(textWriter, ThisTypeInstance);
textWriter.Close();
}
serializer = null;
}
// SerializeToXML
public static System.Collections.Generic.List<T> DeserializeXmlFromFileAsList<T>(string strFileNameAndPath)
{
System.Xml.Serialization.XmlSerializer deserializer = new System.Xml.Serialization.XmlSerializer(typeof(System.Collections.Generic.List<T>));
System.Collections.Generic.List<T> ThisTypeList = null;
using (System.IO.StreamReader srEncodingReader = new System.IO.StreamReader(strFileNameAndPath, System.Text.Encoding.UTF8)) {
ThisTypeList = (System.Collections.Generic.List<T>)deserializer.Deserialize(srEncodingReader);
srEncodingReader.Close();
}
deserializer = null;
return ThisTypeList;
}
// DeserializeXmlFromFileAsList
#endif
} // End Class Serialization
} // End Namespace COR.Tools.XML
XML 持有者:
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace OpenOffice.ODP.Meta
{
[XmlRoot(ElementName = "document-statistic", Namespace = "urn:oasis:names:tc:opendocument:xmlns:meta:1.0")]
public class cDocumentStatistic
{
[XmlAttribute(AttributeName = "object-count", Namespace = "urn:oasis:names:tc:opendocument:xmlns:meta:1.0")]
public string ObjectCount { get; set; }
}
[XmlRoot(ElementName = "meta", Namespace = "urn:oasis:names:tc:opendocument:xmlns:office:1.0")]
public class Meta
{
[XmlElement(ElementName = "creation-date", Namespace = "urn:oasis:names:tc:opendocument:xmlns:meta:1.0")]
public string CreationDate { get; set; }
[XmlElement(ElementName = "editing-duration", Namespace = "urn:oasis:names:tc:opendocument:xmlns:meta:1.0")]
public string EditingDuration { get; set; }
[XmlElement(ElementName = "editing-cycles", Namespace = "urn:oasis:names:tc:opendocument:xmlns:meta:1.0")]
public string EditingCycles { get; set; }
[XmlElement(ElementName = "date", Namespace = "http://purl.org/dc/elements/1.1/")]
public string Date { get; set; }
[XmlElement(ElementName = "document-statistic", Namespace = "urn:oasis:names:tc:opendocument:xmlns:meta:1.0")]
public cDocumentStatistic DocumentStatistic { get; set; }
[XmlElement(ElementName = "generator", Namespace = "urn:oasis:names:tc:opendocument:xmlns:meta:1.0")]
public string Generator { get; set; }
}
[XmlRoot(ElementName = "document-meta", Namespace = "urn:oasis:names:tc:opendocument:xmlns:office:1.0")]
public class DocumentMeta
{
[XmlElement(ElementName = "meta", Namespace = "urn:oasis:names:tc:opendocument:xmlns:office:1.0")]
public Meta Meta { get; set; }
[XmlAttribute(AttributeName = "meta", Namespace = "http://www.w3.org/2000/xmlns/")]
public string _Meta { get; set; }
[XmlAttribute(AttributeName = "office", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Office { get; set; }
[XmlAttribute(AttributeName = "xlink", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Xlink { get; set; }
[XmlAttribute(AttributeName = "dc", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Dc { get; set; }
[XmlAttribute(AttributeName = "presentation", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Presentation { get; set; }
[XmlAttribute(AttributeName = "ooo", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Ooo { get; set; }
[XmlAttribute(AttributeName = "smil", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Smil { get; set; }
[XmlAttribute(AttributeName = "anim", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Anim { get; set; }
[XmlAttribute(AttributeName = "grddl", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Grddl { get; set; }
[XmlAttribute(AttributeName = "officeooo", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Officeooo { get; set; }
[XmlAttribute(AttributeName = "version", Namespace = "urn:oasis:names:tc:opendocument:xmlns:office:1.0")]
public string Version { get; set; }
[XmlAttribute(AttributeName = "transformation", Namespace = "http://www.w3.org/2003/g/data-view#")]
public string Transformation { get; set; }
}
}
附加问题:
有人知道持续时间格式是什么吗?