0

I'm using C# (WPF).
I have the following XML file:

<?xml version="1.0" encoding="utf-8"?>
<ApplicationToRun IsFromXML="true">
<Apps>
    <FulllPath>c:\file1.txt</FullPath>
    <FulllPath>c:\file2.txt</FullPath>
    <FulllPath>c:\file3.txt</FullPath>
</Apps>

</ApplicationToRun>

And i try to deseriazlier the xml file.
My Code:

mlSerializer xs = new XmlSerializer(typfof(MyXMLClass));
StringReader st = new StringReader(xmlPath);

MyXMLClass apps = (MyXMLClass)xs.Deserialize(st); //Exception - System.Windows.Markup.XamlParseException


Inside the inner exception: System.InvalidOperationException :There is an error in XML document (1.1) ...

My Classes:

[XmlRootAttriute("ApplicationToRun:)
public class MyXMLClass
{
    [XmlAttribute]
    public bool IsFromXML {get;set;}
    [XmlElement("Apps")]
    public FullPath [] fullPath {get;set;}
}

public class FullPath
{
    public stirng fullPathApp {set;get;}
}

Where is my mistake?
Thanks!

  • 2
    Apart from the transcription errors? You didn't include the exception text, and `fullPath` should be of type `string[]`. – Mitch Feb 08 '16 at 17:07
  • @Mitch, Exception - System.Windows.Markup.XamlParseException, i tried to replace fullPath to string[] but its still throw exception –  Feb 08 '16 at 17:09
  • That will have an inner exception which gives the actual issue. `XamlParseException` just means there was an issue when instantiating your app. – Mitch Feb 08 '16 at 17:10
  • amm..whats mean and how can i fix it? –  Feb 08 '16 at 17:12
  • See [System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll?](http://stackoverflow.com/a/22189500/138200) – Mitch Feb 08 '16 at 17:14
  • oh ok, thats because i'm tried to deseriazlier from the xml in my constructor, no? –  Feb 08 '16 at 17:15
  • @Mitch, i edited the post, the "full" exception is: ystem.InvalidOperationException :There is an error in XML document (1.1) ... –  Feb 08 '16 at 17:20

2 Answers2

2

There's a typo in the elements. The opening elements have 3 Ls (FulllPath) but the closing ones have 2 Ls (as they should) that's causing the XML error

Volkan Paksoy
  • 6,727
  • 5
  • 29
  • 40
2

The XML is badly formed, but all it takes is a bit of cleaning up:

<?xml version="1.0" encoding="utf-8"?>
<ApplicationToRun IsFromXML="true">
    <Apps>
        <FullPath>foo</FullPath>
        <FullPath>bar</FullPath>
    </Apps>
</ApplicationToRun>

Then assign the correct XML attributes to your class:

[XmlRootAttribute("ApplicationToRun")]
public class MyXMLClass
{
    [XmlAttribute]
    public bool IsFromXML { get; set; }
    [XmlArray("Apps")]
    [XmlArrayItem("FullPath")]
    public string[] fullPath { get; set; }
}
Chima Osuji
  • 391
  • 2
  • 10
  • 2
    In addition to the above, replace the StringReader with the StreamReader, otherwise you will get the following error: There is an error in XML document (1, 1). – Justin Bannister Feb 08 '16 at 17:48