将XML属性添加到序列化的类属性

时间:2013-04-10 14:51:12

标签: c# xml xmlserializer

我见过一个使用包装类来做这个的例子,但我想知道是否有更好(更简单)的方法。

我的课程如下:

public class PartData
{
  public List<PartInfo> PartList { get; set; }
}


public class PartInfo
{
  public string PartNumber { get; set; }

  public string OEMNumbers { get; set; }

  public List<VehicleApplication> VehicleApplications { get; set; }
}


public class VehicleApplication
{

  public string Year { get; set; }

  public string Make { get; set; }

  public string Model { get; set; }
}

序列化代码:

//serialize
private void serialize()
{
   PartData p = GetParts();
   SerializeClass(p);
}

public void SerializeClass(object instance)
{
  var serializer = new XmlSerializer(typeof(PartData));

  using (var writer = new StreamWriter("C:\\TestFile.xml"))
  {
    serializer.Serialize(writer, instance);
  }
}

序列化时,我得到以下输出:

 <PartData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <PartList>
    <PartInfo>
      <PartNumber>12345</PartNumber>
      <OEMNumbers>14556, 14557, 14558, 14559</OEMNumbers>
      <VehicleApplications>
        <VehicleApplication>
          <Year>2001</Year>
          <Make>Ford</Make>
          <Model>F150</Model>
        </VehicleApplication>
        <VehicleApplication>
          <Year>2001</Year>
          <Make>Ford</Make>
          <Model>F150</Model>
        </VehicleApplication>
        <VehicleApplication>
          <Year>2001</Year>
          <Make>Ford</Make>
          <Model>F150</Model>
        </VehicleApplication>
        <VehicleApplication>
          <Year>2001</Year>
          <Make>Ford</Make>
          <Model>F150</Model>
        </VehicleApplication>
      </VehicleApplications>
    </PartInfo>
  </PartList>
</PartData>

理想情况下,我想让PartNumber成为这样的属性:

 <PartData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <PartList>
    <PartInfo>
      <PartNumber Id ="12345">
      <OEMNumbers>14556, 14557, 14558, 14559</OEMNumbers>
      <VehicleApplications>
        <VehicleApplication>
          <Year>2001</Year>
          <Make>Ford</Make>
          <Model>F150</Model>
        </VehicleApplication>
        <VehicleApplication>
          <Year>2001</Year>
          <Make>Ford</Make>
          <Model>F150</Model>
        </VehicleApplication>
        <VehicleApplication>
          <Year>2001</Year>
          <Make>Ford</Make>
          <Model>F150</Model>
        </VehicleApplication>
        <VehicleApplication>
          <Year>2001</Year>
          <Make>Ford</Make>
          <Model>F150</Model>
        </VehicleApplication>
      </VehicleApplications>
    </Partnumber>
    </PartInfo>
  </PartList>
</PartData>

我尝试添加:

public class PartInfo
{
  [XmlAttribute("Id")]
  public string PartNumber { get; set; }

  public string OEMNumbers { get; set; }

  public List<VehicleApplication> VehicleApplications { get; set; }
}

但创建:

<PartInfo Id="12345">

当我需要时:

<PartNumber Id="12345">

1 个答案:

答案 0 :(得分:2)

我认为这是实现目标的唯一途径

public class PartDetail
{
  [XmlAttribute]
  public string ID{ get; set; }

  public string OEMNumbers { get; set; }

  public List<VehicleApplication> VehicleApplications { get; set; }
}

public class PartInfo
{
  public PartDetail { get; set; }
}

在回答您的第一条评论时,这是实现它的最简单方法,另一种方法是编写自定义序列化程序