反序列化XML时出错

时间:2015-06-16 08:31:44

标签: c# xml serialization deserialization

我正在序列化的类:

public class StyleProperties
{
    [XmlAttribute]
    public string Name;
    [XmlAttribute]
    public string Description;
    [XmlAttribute]
    public StyleType Type;

    public ShapeStyleProperties ShapeStyle;
    public TextStyleProperties TextStyle;
}

负责序列化数据的部分:

XmlSerializer serializer = new XmlSerializer(typeof(StyleProperties));

PPT.Presentation pres = Globals.ThisAddIn.Application.ActivePresentation;
CustomXMLParts parts = pres.CustomXMLParts;

//var serializer = new DataContractSerializer(typeof(MyCustomData));
using (var stream = new MemoryStream())
{
    serializer.Serialize(stream, styleProperties);
    stream.Seek(0, SeekOrigin.Begin);

    var sr = new StreamReader(stream);
    var myStr = sr.ReadToEnd();
    Console.WriteLine(myStr);

    parts.Add(myStr);

}

负责反序列化的部分:

    public static T ReturnObjectFromXML<T>(CustomXMLPart customXMLPart)
    {

        using (XmlReader reader = XmlReader.Create(new StringReader(customXMLPart.XML)))
        {
            DataContractSerializer formatter0 =
                new DataContractSerializer(typeof(T));

            return (T)formatter0.ReadObject(reader);
        }        

   }

当然在行

中发生错误
return (T)formatter0.ReadObject(reader);

我得到的错误:

  

第1行位置出错23.期待元素'StyleProperties'来自   命名空间'http://schemas.datacontract.org/2004/07/PPT_Styles_Tool'..   遇到名为'StyleProperties'的'元素',命名空间''。

XML:

<?xml version="1.0"?><StyleProperties xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Name="sdfdzdfvvc" Description="" Type="Shape">
  <ShapeStyle>
    <Location>
      <Left>521.6249</Left>
      <Top>37.75</Top>
    </Location>
    <Size>
      <Width>198.125</Width>
      <Height>54</Height>
    </Size>
    <Fill>
      <Color>
        <SchemeColor>ppFill</SchemeColor>
        <ObjectThemeColor>msoThemeColorAccent1</ObjectThemeColor>
        <ColorType>msoColorTypeScheme</ColorType>
      </Color>
      <Transparency>System.__ComObject</Transparency>
      <Visible>msoFalse</Visible>
      <FillType>msoFillSolid</FillType>
    </Fill>
    <AutoShapeType>msoShapeNotPrimitive</AutoShapeType>
    <Rotation>0</Rotation>
    <LockAspectRatio>msoFalse</LockAspectRatio>
    <BlackWhiteMode>msoBlackWhiteAutomatic</BlackWhiteMode>
    <TextFrame>
      <MarginTop>0</MarginTop>
      <MarginLeft>0</MarginLeft>
      <MarginBottom>0</MarginBottom>
      <MarginRight>0</MarginRight>
    </TextFrame>
    <TextFrame2>
      <WordWrap>msoTrue</WordWrap>
    </TextFrame2>
    <LineStyle>
      <DashStyle>msoLineSolid</DashStyle>
      <Transparency>0</Transparency>
      <Weight>0.75</Weight>
      <Color>
        <SchemeColor>ppForeground</SchemeColor>
        <ObjectThemeColor>msoThemeColorText1</ObjectThemeColor>
        <ColorType>msoColorTypeScheme</ColorType>
      </Color>
      <Visible>msoFalse</Visible>
      <LineStyle>msoLineSingle</LineStyle>
      <LinePattern>msoPatternMixed</LinePattern>
    </LineStyle>
    <ShapeTFAutoSize>ppAutoSizeNone</ShapeTFAutoSize>
  </ShapeStyle>
  <TextStyle>
    <Font/>
    <ParagraphFormat/>
  </TextStyle>
</StyleProperties>

所以从我看来没有命名空间:'http://schemas.datacontract.org/2004/07/PPT_Styles_Tool'但是我该如何修复呢?我是处理XML文档的新手,特别是将XML数据序列化/反序列化到对象

1 个答案:

答案 0 :(得分:1)

使用[XmlElement]属性:

[XmlElement(ElementName = "StyleProperties", Namespace="http://schemas.datacontract.org/2004/07/PPT_Styles_Tool")]
public class StyleProperties
{
//...

此外,如果使用XmlSerializer进行序列化,则应使用XmlSerializer进行反序列化。不是DataContractSerializer。