代码之家  ›  专栏  ›  技术社区  ›  E Rolnicki

.NET XML序列化:具有属性的整数元素?

  •  1
  • E Rolnicki  · 技术社区  · 14 年前

    是否有可能在C中实现以下目标?

    下节课…

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

    我希望实现以下XML:

    <Foo>
      <BarId BarString="something">123</BarId>
    </Foo>
    
    2 回复  |  直到 14 年前
        1
  •  4
  •   reSPAWNed    14 年前

    arsenmkrt在正确的轨道上,但缺少元素的内容,我建议修订版本:

    class BarId
    {
        [XmlText()]
        public int Content {get; set;}
    
        [XmlAttribute()]
        public string BarString {get; set;}
    }
    
    public class Foo{
        public BarId BarId {get; set;}
    }
    

    这样,您就可以获得整数形式的内容。

        2
  •  0
  •   Community leo1    7 年前

    您应该创建一个barid类,其中包含barstring

    class BarId
    {
        [XmlAttribute]
        public string BarString{get;set;}
    }
    
    public class Foo{
     public BarId BarId{get;set;}
    }
    

    或者可以使用自定义序列化机制,例如 here