c# - .NET Xml 序列化 : Integer Element with Attribute?

标签 c# xml-serialization

是否有可能在c#中实现如下...

对于下面的类...

public class Foo{
 public int BarId{get;set;}
 public string BarString{get;set;}
}

我想实现以下 XML:

<Foo>
  <BarId BarString="something">123</BarId>
</Foo>

最佳答案

ArsenMkrt 是对的,但是缺少元素的内容,我建议修改版本:

class BarId
{
    [XmlText()]
    public int Content {get; set;}

    [XmlAttribute()]
    public string BarString {get; set;}
}

public class Foo{
    public BarId BarId {get; set;}
}

通过这种方式,您可以获取整数形式的内容。

关于c# - .NET Xml 序列化 : Integer Element with Attribute?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3309539/

相关文章:

c# - FileSystemWatcher:忽略自己进程所做的更改

c# - 在单个 Razor View 中,如何检索从 Controller 的不同方法传递的值?

c# - C#中通过XmlSerializer类反序列化多个同名XML元素

c# - StdOut 充斥着 'C:\fakepath' 警告,整数除法可能会慢得多,如果可能,请尝试使用 uints 通过 C# 使用 Selenium

c# - 慢正则表达式拆分

C# Xml 序列化 : can't (de)serialize an object that comes from another assembly

c# - 表示不被序列化的类的属性

c# - 在不改变原始类的情况下,在序列化过程中排除一些属性

c# - 使用 Xsd 验证序列化 WCF 代理类中的 Xml 时缺少架构错误

c# - 如何在 MVC 2 aspx 中使用 C# 显示和隐藏 Div