我试图让我的 XML 看起来像下面那样
<Assets>
<Asset id="" Type="" name="" filename="">filepath</Asset>
<Asset id="" Type="" name="" filename="">filepath</Asset>
</Assets>
我现在拥有的是
public class Main
{
public List<Asset> Assets { get; set; }
}
public class Asset
{
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
[XmlAttribute(AttributeName = "Type")]
public string Type { get; set; }
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
[XmlAttribute(AttributeName = "filename")]
public string FileName { get; set; }
public string FilePath { get; set; }
}
Main 类并不重要,这是来自较大对象的一小段。
我遇到的问题是让文件路径成为 Assets 元素的值
在当前状态下,XML 看起来像
<Assets>
<Asset id="" Type="" name="" filename="">
<FilePath>filepath</FilePath>
</Asset>
<Asset id="" Type="" name="" filename="">
<FilePath>filepath</FilePath>
</Asset>
</Assets>
最佳答案
尝试 XmlTextAttribute
:
[XmlText]
public string FilePath { get; set; }
关于c# XML序列化为元素赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28348271/