0

I'm trying make a program to read a simple xml file into my class. I've been using this previous question as a guide: How to Deserialize XML document

The code runs fine with no exceptions, but for some reason, SCArray.ShortCut = null and count is 0. I'm having trouble debugging this because there are no exceptions.

Is there a way to catch this error (i.e., why it's not reading the xml correctly, or what part of my code is causing it to return null results from reading the xml)?

<?xml version="1.0"?>
<ShortCutsArray>
    <Shortcut>
        <Name>Item1</Name>
        <Path>http://www.example1.com</Path>
    </Shortcut>
    <Shortcut>
        <Name>Item2</Name>
        <Path>\\Server\example2\ex2.exe</Path>
    </Shortcut>
</ShortCutsArray>

The c# code:

class Program
{
    public static void Main(string[] args)
    {
        string shortcuts_file = @"\\server\ShortcutLocation.xml";
        ShortCutsArray SCArray = null;
        XmlSerializer serializer = new XmlSerializer(typeof(ShortCutsArray));
        StreamReader reader = new StreamReader(shortcuts_file);
        SCArray = (ShortCutsArray)serializer.Deserialize(reader);
        reader.Close();
    }
}
[Serializable()]
[System.Xml.Serialization.XmlRoot("ShortCutsArray")]
public class ShortCutsArray
{
    [XmlArray("ShortCutsArray")]
    [XmlArrayItem("ShotCut", typeof(ShortCut))]
    public ShortCut[] ShortCuts { get; set; }
}
[Serializable()]
public class ShortCut
{
    [System.Xml.Serialization.XmlElement("Name")]
    public string Name { get; set; }
    [System.Xml.Serialization.XmlElement("Path")]
    public string Path { get; set; }
}
Community
  • 1
  • 1
Alex
  • 689
  • 1
  • 8
  • 22
  • Looks like a typo here: `[XmlArrayItem("ShotCut", typeof(ShortCut))]` should be `[XmlArrayItem("ShortCut", typeof(ShortCut))]` (ShotCut --> ShortCut) – David LaPorte Jan 15 '15 at 14:41
  • Mostly correct: ShotCut -> Shortcut (note the small c as well) – J. Steen Jan 15 '15 at 14:43
  • Also possible duplicate of http://stackoverflow.com/questions/15258818/deserialize-xml-array-where-root-is-array-and-elements-dont-follow-conventions – J. Steen Jan 15 '15 at 14:44
  • Thanks! Unfortunately, same results. – Alex Jan 15 '15 at 14:44
  • @J.Steen my question is how to debug this though – Alex Jan 15 '15 at 14:45
  • Well. Your code is not incorrect in the way that exceptions will be thrown. It won't throw an exception because it expects another XML as far as your model is concerned. You can't really debug the XmlSerializer unless you download the sourcecode, copy it into your own project, and step through that. – J. Steen Jan 15 '15 at 14:46
  • I made the changes from the link I posted in a test-project, and your xml is successfully deserialized. – J. Steen Jan 15 '15 at 14:48
  • Also - just found this. Might be of use in the future: http://www.hanselman.com/blog/HOWTODebugIntoANETXmlSerializerGeneratedAssembly.aspx – J. Steen Jan 15 '15 at 14:49
  • Thanks @J.Steen that answers all of my questions. If you post an answer I'll accept it. – Alex Jan 15 '15 at 14:54

1 Answers1

0

Typo: ShotCut -> Shortcut (note the lower case c - either making the ShortCut class Shortcut or change you xml to be ShortCut would be better)

You've got the root as ShortCutsArray and the XML array name as ShortCutsArray. You'd need the following xml for it to work:

<ShortCutsArray>
    <ShortCutsArray>
        <Shortcut>
            <Name>Item1</Name>
            <Path>http://www.example1.com</Path>
        </Shortcut>
        <Shortcut>
            <Name>Item2</Name>
            <Path>\\Server\example2\ex2.exe</Path>
        </Shortcut>
    </ShortCutsArray>
</ShortCutsArray>

I'm not sure its possible to debug this as there's nothing going wrong you've simply found no elements that match due to the above errors.

Tim Rutter
  • 4,549
  • 3
  • 23
  • 47