【问题标题】:XmlSerializer : formatting tagsXmlSerializer : 格式化标签
【发布时间】:2012-07-12 11:17:11
【问题描述】:

我使用 .NET XmlSerializer 简单地序列化具有 Items 作为集合的 Person;

class Item
{
   Name
   Price
}

class Person
{
   Name
   List Items<Item>
}

一切都很好...我使用 XmlWriterSettings 来缩进我的 xml 文件。输出是:

   <?xml version="1.0" encoding="utf-8"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <name>TestName</name>
  <Items>
    <Item>
      <name>one</name>
      <price>0</price>
    </Item>
    <Item>
      <name>two</name>
      <price>1</price>
    </Item>
  </Items>
</Viewport>

但我想要的是:

<?xml version="1.0" encoding="utf-8"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <name>TestName</name>
  <Items>
     <Item name="one" price="0" />
     <Item name="two" price="1" />
  </Items>
</Viewport>

短时间代替

<Item>
      <name>one</name>
      <price>0</price>
 </Item>

我想把xml写成

<Item name="one" price="0" />

我如何在 .NET(C#) 中做到这一点?

【问题讨论】:

标签: c# .net xml xml-serialization


【解决方案1】:
class Item
{
    [System.Xml.Serialization.XmlAttributeAttribute("name")]
    string Name;
    [System.Xml.Serialization.XmlAttributeAttribute("price")]
    string Price;
}

【讨论】:

    【解决方案2】:

    XmlAttribute 装饰您的NamePrice 属性

    【讨论】:

      猜你喜欢
      • 2011-07-21
      • 2018-04-14
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多