Skip to main content
 首页 » 编程设计

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

2024年02月24日21lyhabc

是否有可能在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;} 
} 

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