Quantcast
Channel: XML, System.Xml, MSXML and XmlLite forum
Viewing all articles
Browse latest Browse all 935

How to serialize differents tag name inside a list

$
0
0

Hi,

I have the following xml with a <Files> element that contains either a list of Filter OR a list of File :

<Files><Filter Name="Fichiers sources"
            Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
            UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"><File RelativePath=".\stdafx.cpp"><FileConfiguration Name="Debug|Win32"><Tool Name="VCCLCompilerTool"
                UsePrecompiledHeader="1" /></FileConfiguration><FileConfiguration Name="Release|Win32"><Tool Name="VCCLCompilerTool"
                UsePrecompiledHeader="1" /></FileConfiguration></File><File RelativePath=".\TestWin32vcproj.cpp"></File></Filter><Filter Name="Fichiers d&#39;en-tête"
            Filter="h;hpp;hxx;hm;inl;inc;xsd"
            UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"><File RelativePath=".\Resource.h"></File><File RelativePath=".\stdafx.h"></File><File RelativePath=".\TestWin32vcproj.h"></File></Filter><Filter Name="Fichiers de ressources"
            Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
            UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"><File RelativePath=".\small.ico"></File><File RelativePath=".\TestWin32vcproj.ico"></File><File RelativePath=".\TestWin32vcproj.rc"></File></Filter><File RelativePath=".\ReadMe.txt"></File></Files>

And for the moment I wrote this :

public class VisualStudioProject
{
    //.... blabla
    [XmlArray("Files")]
    public List<Filter> Files { get; set; }
    ////.... blabla
}


    [XmlRoot("Filter")]
    public class Filter
    {
        [XmlAttribute("Name")]
        public string Name { get; set; }

        [XmlAttribute("Filter")]
        public string AttrFilter { get; set; }

        [XmlAttribute("UniqueIdentifier")]
        public string UniqueIdentifier { get; set; }

        [XmlElement("File")]
        public List<File> Files { get; set; }

        public Filter()
        {
        }
    }

[XmlRoot("Filter")]
    public class Filter
    {
        [XmlAttribute("Name")]
        public string Name { get; set; }

        [XmlAttribute("Filter")]
        public string AttrFilter { get; set; }

        [XmlAttribute("UniqueIdentifier")]
        public string UniqueIdentifier { get; set; }

        [XmlElement("File")]
        public List<File> Files { get; set; }

        public Filter()
        {
        }
    }

    [XmlRoot("File")]
    public class File //: FileItemBase
    {
        [XmlAttribute("RelativePath")]
        public string RelativePath { get; set; }

        [XmlElement("FileConfiguration")]
        public List<FileConfiguration> FileConfigurations { get; set; }
        public bool ShouldSerializeFileConfigurations() { return FileConfigurations.Count > 0; }

        [XmlText]
        public string XmlText { get; set; }

        public File()
        {
            this.XmlText = String.Empty;
            this.FileConfigurations = new List<FileConfiguration>();
        }
    }

But of course with this code I can only catch the Filter elements but not the last File element.

How can I modify my code to catch both ? Is it possible to do it using xml attributes ?

Thanks




Viewing all articles
Browse latest Browse all 935

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>