如何使用c#从xml获取强类型对象?

时间:2014-05-05 19:43:20

标签: c# xml

我创建了一个类似于此的类模型。

public class SiteDefinition
    {
        [XmlAttribute ()]
        public string Name { get; set; }
        [XmlAttribute()]
        public string Version { get; set; }
        public List<MasterPage>? MasterPages { get; set; }
        public List<File>? Files { get; set; }
        public List<PageLayout>? PageLayouts { get; set; }
        public List<Feature>? Features { get; set; }
        public List<ContentType>? ContentTypes { get; set; }
        public List<StyleSheet>? StyleSheets { get; set; }
    }

然后,使用控制台应用程序,我生成了一个如下所示的xml文件:

<?xml version="1.0"?>
<SiteDefinition xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Name="ContosoIntranet" Version="1.0.0.0">
  <MasterPages>
    <MasterPage Url="" Name="seattle.master" LocalFolder=".\seattle.master" ServerFolder="_catalogs/ContosoIntranet/" UIVersion="15" />
  </MasterPages>
</SiteDefinition>

我使用的代码:

static void Main(string[] args)
        {
            var siteDefinition = new SiteDefinition();
            siteDefinition.Name = "ContosoIntranet";
            siteDefinition.Version = "1.0.0.0";
            siteDefinition.MasterPages = new List<SiteProvisioningFramework.MasterPage>()
            {
                new MasterPage(){
                    Name="seattle.master",
                    ServerFolder ="_catalogs/ContosoIntranet/",
                    UIVersion = "15",
                    Url="",
                    LocalFolder = ".MasterPages/seattle.master"
                }
            };


            Utilities.XmlHelper.ObjectToXml(siteDefinition, @".\Sample.xml");
        }

我只是想知道xml会是什么样子,在我的应用程序中,用户(开发人员)会给应用程序一个xml文件,然后我想将它转换为强类型对象:{{1} }

如何以更少的努力(线路)实现这一目标?

SiteDefinition

1 个答案:

答案 0 :(得分:1)

看看下面的

    public class SiteDefinition
    {
        [XmlAttribute]
        public string Name { get; set; }
        [XmlAttribute]
        public string Version { get; set; }
        public List<MasterPage> MasterPages { get; set; }
        public List<File> Files { get; set; }

        public List<PageLayout> PageLayouts { get; set; }
        public List<Feature> Features { get; set; }
        public List<ContentType> ContentTypes { get; set; }
        public List<StyleSheet> StyleSheets { get; set; }
    }

    public class MasterPage
    {
        [XmlAttribute]
        public string Name { get; set; }

        [XmlAttribute]
        public string ServerFolder { get; set; }

        [XmlAttribute]
        public string UIVersion { get; set; }

        [XmlAttribute]
        public string Url { get; set; }

        [XmlAttribute]
        public string LocalFolder { get; set; }
    }

    public class StyleSheet
    {
    }

    public class ContentType
    {
    }

    public class Feature
    {
    }

    public class PageLayout
    {
    }

    public class File
    {
    }

    [Fact]
    public void Test()
    {
        XmlSerializer serializer = new XmlSerializer(typeof(SiteDefinition));
        using (FileStream stream = new FileStream("Data.xml", FileMode.Open))
        {
            var siteDefinition = (SiteDefinition)serializer.Deserialize(stream);
        }
    }

请注意,此表达式不正确public List<MasterPage>? MasterPages引用类型无法为空。这里有关于Nullable Types

的更多信息

此处有关XML Serialization

的其他信息