我正在创建一个对象,该对象将根据XSD进行序列化和验证,以创建XML文件。我遇到的问题是XML中的某些节点是重复的,但名称不同,到目前为止我已经得到以下内容:
public class export
{
public info info { get; set; }
public design design { get; set; }
}
public class info
{
public DateTime exportDateTime { get; set; }
public string duration { get; set; }
public planningSoftware planningSoftware { get; set; }
public exporter exporter { get; set; }
}
public class planningSoftware
{
public string name { get; set; }
public string major { get; set; }
public string minor { get; set; }
public string revision { get; set; }
public string build { get; set; }
}
public class exporter
{
public version version { get; set; }
public module module { get; set; }
}
public class version
{
public string name { get; set; }
public string major { get; set; }
public string minor { get; set; }
public string revision { get; set; }
public string build { get; set; }
}
public class module
{
public string name { get; set; }
public string major { get; set; }
public string minor { get; set; }
public string revision { get; set; }
public string build { get; set; }
}
public static void buildXML()
{
var export = new export()
{
info = new info()
{
exportDateTime = DateTime.Today,
duration = 10,
planningSoftware = new planningSoftware()
{
name = "Planning App",
major = "10",
minor = "4",
revision = "6",
build = "225"
},
exporter = new exporter()
{
version = new version()
{
name = "App Test",
major = "10",
minor = "1",
revision = "0",
build = "201"
},
module = new module()
{
name = "App Test Module",
major = "5",
minor = "2",
revision = "1",
build = "220"
}
}
}
};
var s = new XmlSerializer(typeof(export));
using (var f = File.Open("export.xml", FileMode.Create))
{
s.Serialize(f, export);
}
}
我的问题是,如何最大限度地减少字段重复?我通常可以初始化一个保存值的单独对象,但看起来这会影响序列化。
感谢。
答案 0 :(得分:2)
默认情况下,xml中属性的名称基于属性名称,不是其类型名称。
因此,只需创建一个用于保存名称和版本的类型:
public class VersionedName
{
public string name { get; set; }
public string major { get; set; }
public string minor { get; set; }
public string revision { get; set; }
public string build { get; set; }
}
然后在您的其他类型中声明您的该类型的属性:
public class Export
{
public Info info { get; set; }
}
public class Info
{
public DateTime exportDateTime { get; set; }
public int duration { get; set; }
public VersionedName planningSoftware { get; set; }
public Exporter exporter { get; set; }
}
public class Exporter
{
public VersionedName version { get; set; }
public VersionedName module { get; set; }
}
您可以验证以下内容产生的XML输出与初始代码相同:
var export = new Export()
{
info = new Info()
{
exportDateTime = DateTime.Today,
duration = 10,
planningSoftware = new VersionedName()
{
name = "Planning App",
major = "10",
minor = "4",
revision = "6",
build = "225"
},
exporter = new Exporter()
{
version = new VersionedName()
{
name = "App Test",
major = "10",
minor = "1",
revision = "0",
build = "201"
},
module = new VersionedName()
{
name = "App Test Module",
major = "5",
minor = "2",
revision = "1",
build = "220"
}
}
}
};
var s = new XmlSerializer(typeof(Export));
using (var f = File.Open("export.xml", FileMode.Create))
{
s.Serialize(f, export);
}