简体   繁体   中英

Add XML Attribute to serialized class property

I've seen an example that uses a wrapper class to do this, but i was wondering if there is a better (simpler) way.

My classes are as follows:

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 code:

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

when serialized i get the following output:

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

Ideally i would like to make PartNumber an attribute like so:

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

I've tried adding:

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

  public string OEMNumbers { get; set; }

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

but creates:

<PartInfo Id="12345">

when i need:

<PartNumber Id="12345">

I think this is the only way to achieve what you are trying to achieve

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

In answer to your first comment, this is the easiest way to achieve it, the alternative is to write a custom serializer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM