Click here to Skip to main content
14,977,931 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am trying to deserialize a CodeSnippets file and the Root node contains an xmlns namespace and I'm getting the following error;

{"<CodeSnippets xmlns='http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet'> was not expected."}

Snippets file
XML
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">


This is the last thing I've tried but I've tried quite a few things and nothing worked. I even converted the .snippet file to an .xsd then tried to use xsd.exe to transform back to .cs and it wouldn't do it because of the xmlns attribute. WTF???

C#
[Serializable]
[XmlRootAttribute("CodeSnippets", Namespace="xmlns", IsNullable = false)]
public class SnippetItem
{
    public SnippetItem()
    {
        Header = new SnippetHeader();
    }


This is just a small portion of the class, I didn't add the whole thing because the attribute is the problem.

And this is the code I'm using to deserialize;

internal static SnippetItem DeserializeItem(string file)
{
    SnippetItem item = null;

    try
    {
        XmlSerializer deserializer = new XmlSerializer(typeof(SnippetItem));
        TextReader reader = new StreamReader(file);
        item = deserializer.Deserialize(reader) as SnippetItem;
        reader.Close();
    }
    catch { }

    return item;
}
Posted
Updated 20-Mar-11 14:13pm
v3

I do not know if what you are trying to do is possible, or not.

I do know that all of the source code I have seen uses XmlDocument for loading and saving them and XmlNamespaceManager[^] to deal with the xmlnamespaces thereafter.

If you are interested, then download the source for Snippet Designer[^] from CodePlex to see how they do it.
   
Thanks Henry it's interesting because the Extension I am writing is a CodeSnippet Viewer/Editor for the new AvrStudio 5.0[^] and I was fashioning it after the SnippetDesigner so thanks for the link.

Interestingly I found a solution if you replace this;
C#
[Serializable]
[XmlRootAttribute("CodeSnippets", Namespace="xmlns", IsNullable = false)]
public class SnippetItem
{
    public SnippetItem()
    {
        Header = new SnippetHeader();
    }


For this it works but this assumes that the namespace will be there if not you get the same type of exception as without
[XmlType(Namespace = "http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet")]
[XmlRoot(ElementName = "CodeSnippets", Namespace = "http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet", IsNullable = false)]
public class SnippetItem
{
    public SnippetItem()
    {
        Header = new SnippetHeader();
    }
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900