C# Xml将类的实例序列化/反序列化为';丑陋';XML格式
我有一个简单的课程:C# Xml将类的实例序列化/反序列化为';丑陋';XML格式,c#,xml-serialization,C#,Xml Serialization,我有一个简单的课程: public class SomeClass { public int SomeInt { get; set; } public string SomeString { get; set; } [XmlArrayItem("AString")] public List<string> SomeStrings { get; set; } } 公共类SomeClass { 公共int SomeInt{get;set;} 公共字符串
public class SomeClass
{
public int SomeInt { get; set; }
public string SomeString { get; set; }
[XmlArrayItem("AString")]
public List<string> SomeStrings { get; set; }
}
公共类SomeClass
{
公共int SomeInt{get;set;}
公共字符串SomeString{get;set;}
[XmlArrayItem(“AString”)]
公共列表SomeStrings{get;set;}
}
我需要将此类的实例序列化为格式不正确的xml(我无法更改)。如果按原样序列化该类,则会得到以下结果:
<?xml version="1.0" encoding="utf-8"?>
<SomeClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SomeInt>1234</SomeInt>
<SomeString>Hello</SomeString>
<SomeStrings>
<AString>One</AString>
<AString>Two</AString>
<AString>Three</AString>
</SomeStrings>
</SomeClass>
1234
你好
一
二
三
我想要得到的是以下内容(AString元素不包含在父元素中):
1234
你好
一
二
三
我在List属性上尝试了Xml*属性的各种组合,但它总是希望写入父元素(SomeStrings)
除了实现IXmlSerializable接口之外,还有什么方法可以修改类以实现我想要的结果吗?请尝试使用
[XmlElement]
属性:
public class SomeClass
{
public int SomeInt { get; set; }
public string SomeString { get; set; }
[XmlElement]
public List<string> SomeStrings { get; set; }
}
公共类SomeClass
{
公共int SomeInt{get;set;}
公共字符串SomeString{get;set;}
[XmlElement]
公共列表SomeStrings{get;set;}
}
想要的XML有什么格式不好?@JohnSaunders-我只是不喜欢没有容器元素的重复元素。我更喜欢xml的第一个版本。在我看来,即使第二个版本在技术上格式良好,第一个版本也会更好。好的,术语“格式良好”在XML中是一个特定的技术术语,而不是您使用的术语。我相信你想要的术语是“丑陋”。这很管用!我将其更改为[xmlement(“AString”)]
以获得我想要的元素名称。
public class SomeClass
{
public int SomeInt { get; set; }
public string SomeString { get; set; }
[XmlElement]
public List<string> SomeStrings { get; set; }
}