1

我看过一个使用包装类来执行此操作的示例,但我想知道是否有更好(更简单)的方法。

我的课程如下:

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">
4

1 回答 1

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; }
}

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

于 2013-04-10T15:30:37.013 回答