【发布时间】:2014-11-26 19:09:02
【问题描述】:
我有一个简单的方法,它使用 C# .Net 类将对象列表序列化为 XML 字符串。对于该方法,我想从生成的 XML 字符串中删除 xml 元素。请帮忙。谢谢。
我的方法:
public static string CreateXML(List<Video> list)
{
string result = "";
StringWriter stringWriter = new StringWriter();
XmlSerializer s = new XmlSerializer(list.GetType());
s.Serialize(stringWriter, list);
result = stringWriter.ToString();
return result;
}
我从那个方法得到的结果字符串(为简洁起见,我删掉了):
<?xml version="1.0" encoding="utf-16" ?>
<ArrayOfVideo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Video>-
....
</Video>
<Video>
....
</Video>
</ArrayOfVideo>
我想要一个结果 xml 字符串,例如:(添加标签 <Videos> and </Videos>)
<Videos>
<Video>-
....
</Video>
<Video>
....
</Video>
</Videos>
所以,我想删除行:
<?xml version="1.0" encoding="utf-16" ?>
<ArrayOfVideo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
</ArrayOfVideo>
【问题讨论】:
-
看看这个SO post是否有帮助。
标签: c# xml-serialization