Class that I am serializing:
public class StyleProperties { [XmlAttribute] public string Name; [XmlAttribute] public string Description; [XmlAttribute] public StyleType Type; public ShapeStyleProperties ShapeStyle; public TextStyleProperties TextStyle; } Part that is responsible for serializing data:
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); } Part that is responsible for deserializing:
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); } } Error occurs of course in line
return (T)formatter0.ReadObject(reader); Error which I am getting:
Error in line 1 position 23. Expecting element 'StyleProperties' from namespace 'http://schemas.datacontract.org/2004/07/PPT_Styles_Tool'.. Encountered 'Element' with name 'StyleProperties', namespace ''.
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> So from that what I see there is no namespace: 'http://schemas.datacontract.org/2004/07/PPT_Styles_Tool' But how can I repair it? I am rookie at working with XML documents and especially serializing/deserializing XML data to/from objects
\".XmlSerializerand then trying to read it back withDataContractSerializer. You should pick one or the other.