1

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

2
  • Please edit your post to include the real XML. It looks like you got that from the debugger, and it includes escape characters \". Commented Jun 16, 2015 at 8:37
  • You're serializing with XmlSerializer and then trying to read it back with DataContractSerializer. You should pick one or the other. Commented Jun 16, 2015 at 8:44

1 Answer 1

1

Use the [XmlElement] attribute:

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

Also, if you use the XmlSerializer to serialize, then you should use the XmlSerializer to deserialize. Not the DataContractSerializer.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! It works! I've just had to change XmlElement into XMLRoot, because XMLElement was not accepted by debugger.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.