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; }
}
9
  • Looks like a typo here: [XmlArrayItem("ShotCut", typeof(ShortCut))] should be [XmlArrayItem("ShortCut", typeof(ShortCut))] (ShotCut --> ShortCut) Commented Jan 15, 2015 at 14:41
  • Mostly correct: ShotCut -> Shortcut (note the small c as well) Commented Jan 15, 2015 at 14:43
  • Also possible duplicate of stackoverflow.com/questions/15258818/… Commented Jan 15, 2015 at 14:44
  • Thanks! Unfortunately, same results. Commented Jan 15, 2015 at 14:44
  • @J.Steen my question is how to debug this though Commented Jan 15, 2015 at 14:45

1 Answer 1

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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.