c# XML序列化为元素赋值

标签 c# xml xml-serialization

我试图让我的 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/

相关文章:

c# - 反序列化具有相同元素名称但不同属性的 Xml

c# - 在 C# 中生成随机小数

c# - Windows 8.1 应用程序中的 Toast 通知

c# - C# 中的 Unix 时间转换

java - 在Java中创建具有多个子节点的XML文件

java - 如何在 JAXB xml 中隐藏 "null=' true'"

python - 使用 lxml.objectify 测试元素是否存在

c# - 在 .NET Framework 2 和 4 中进行序列化和反序列化 - C#

c# - 无法在 Web 应用程序中找到路径

perl - XML::Dumper 在输出中使用错误的哈希引用