Serializing Object to XML and removing unwanted text from XML
I have an object that I want to serialize to XML and I am using the following code to carry this out:
public static string Serialize(object obj)
{
using (var memoryStream = new MemoryStream())
using (var reader = new StreamReader(memoryStream))
{
var serializer = new DataContractSerializer(obj.GetType());
serializer.WriteObject(memoryStream, obj);
memoryStream.Position = 0;
return reader.ReadToEnd();
}
}
and when I do, I get the following XML:
<TestRequestPOCO xmlns=\"http://schemas.datacontract.org/2004/07/GPTR.Model.POCOs\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">
<AdditionalInformation>Additional Information</AdditionalInformation>
<AddressLine1>6 MOUNT PLEASANT ROAD</AddressLine1>
<AddressLine2>LEEDS</AddressLine2>
<AddressLine3 i:nil=\"true\"/>
<AddressLine4 i:nil=\"true\"/>
<AntibioticThe开发者_运维百科rapy i:nil=\"true\"/>
<ClinicalInformation>Clinical Information</ClinicalInformation>
<ClinicalInformationXml i:nil=\"true\"/>
<Clinician>Dr NM BRYAN</Clinician>
<ClinicianCode>4203845</ClinicianCode>
<ClinicianShortCode :nil=\"true\"/>
<Destination>1</Destination>
<Dob>1992-02-29T00:00:00</Dob>
<ExpectedDate>2011-10-06T10:22:57.096+01:00</ExpectedDate>
<FirstName>ALISON</FirstName>
<GenerateOrder>true</GenerateOrder>
<HospitalNumber i:nil=\"true\"/>
<IsFasting>false</IsFasting>
<IsPrivatePatient>false</IsPrivatePatient>
<IsSensitive>false</IsSensitive>
<IsUrgent>false</IsUrgent>
<Items>
<RequestDataItem>
<AdditionalInfo i:nil=\"true\"/>
<Container i:nil=\"true\"/>
<Description>Ferritin [Serum]</Description>
<LIMSDeptName>CHM</LIMSDeptName>
<ProfileNumber>1293</ProfileNumber>
<QualifierCode i:nil=\"true\"/>
<SiteCode i:nil=\"true\"/>
<SpecimenType>Serum</SpecimenType>
<UniqueTest>False</UniqueTest>
</RequestDataItem>
</Items>
<Location>0</Location>
<LocationName>The INPS Practice</LocationName>
<LocationShortCode>W97046</LocationShortCode>
<LocationTelephone>01792602898</LocationTelephone>
<MiddleName i:nil=\"true\"/>
<NhsNumber>5240022631</NhsNumber>
<OrgCode>RRS</OrgCode>
<Placer>Dr Sarah Saturn</Placer>
<PostCode>CF31 5EP</PostCode>
<Sex>Male</Sex>
<Source>GPTR</Source>
<SurName>WILLIAMS</SurName>
<TelephoneNumber>01792776776</TelephoneNumber>
</TestRequestPOCO>"
As you can see,it doesn't handle empty tags well and also I want to strip out the text in the root tag so I end up with something like this:
<TestRequestPOCO>
<AdditionalInformation>Additional Information</AdditionalInformation>
<AddressLine1>6 MOUNT PLEASANT ROAD</AddressLine1>
<AddressLine2>LEEDS</AddressLine2>
<AddressLine3/>
<AddressLine4/>
<AntibioticTherapy>
<ClinicalInformation>Clinical Information</ClinicalInformation>
<ClinicalInformationXml/>
<Clinician>Dr NM BRYAN</Clinician>
<ClinicianCode>4203845</ClinicianCode>
<ClinicianShortCode/>
<Destination>1</Destination>
<Dob>1992-02-29T00:00:00</Dob>
<ExpectedDate>2011-10-06T10:22:57.096+01:00</ExpectedDate>
<FirstName>ALISON</FirstName>
<GenerateOrder>true</GenerateOrder>
<HospitalNumber/>
<IsFasting>false</IsFasting>
<IsPrivatePatient>false</IsPrivatePatient>
<IsSensitive>false</IsSensitive>
<IsUrgent>false</IsUrgent>
<Items>
<RequestDataItem>
<AdditionalInfo/>
<Container/>
<Description>Ferritin [Serum]</Description>
<LIMSDeptName>CHM</LIMSDeptName>
<ProfileNumber>1293</ProfileNumber>
<QualifierCode/>
<SiteCode/>
<SpecimenType>Serum</SpecimenType>
<UniqueTest>False</UniqueTest>
</RequestDataItem>
</Items>
<Location>0</Location>
<LocationName>The INPS Practice</LocationName>
<LocationShortCode>W97046</LocationShortCode>
<LocationTelephone>01792602898</LocationTelephone>
<MiddleName />
<NhsNumber>5240022631</NhsNumber>
<OrgCode>RRS</OrgCode>
<Placer>Dr Sarah Saturn</Placer>
<PostCode>CF31 5EP</PostCode>
<Sex>Male</Sex>
<Source>GPTR</Source>
<SurName>WILLIAMS</SurName>
<TelephoneNumber>01792776776</TelephoneNumber>
</TestRequestPOCO>"
Does anyone have any idea how I can strip these out and come up with a string of XML like above.
Thanks in advance.
To remove the 'null' values (i.e., the values that have i:nil set to true), you need to set the "EmitDefaultValue" on the DataMemberAttribute of those data members to false. In the .NET Framework, types have a concept of default values. For example, for any reference type the default value is null, and for an integer type it is 0.
Here's an example:
[DataContract]
public class Employee
{
// The CLR default for as string is a null value.
// This will be written as <employeeName xsi:nill="true" />
[DataMember]
public string EmployeeName = null;
// This will be written as <employeeID>0</employeeID>
[DataMember]
public int employeeID = 0;
// The next three will not be written because the EmitDefaultValue = false.
[DataMember(EmitDefaultValue = false)]
public string position = null;
[DataMember(EmitDefaultValue = false)]
public int salary = 0;
[DataMember(EmitDefaultValue = false)]
public int? bonus = null;
// This will be written as <targetSalary>57800</targetSalary>
[DataMember(EmitDefaultValue = false)]
public int targetSalary = 57800;
}
I'd make sure you think carefully before you do this. The reason is that when EmitDefaultValue is set to false, it is represented in the schema as an annotation specific to Windows Communication Foundation (WCF). There is no interoperable way to represent this information. In particular, the "default" attribute in the schema is not used for this purpose, the minOccurs attribute is affected only by the IsRequired setting, and the nillable attribute is affected only by the type of the data member.
As for removing the various other extraneous XML attributes, you can never totally remove it, but something like the following code may help you ensure that it is emitted only once at the top level:
Notice how the highlighted namespace was defined five times, creating a lot of bloat when it could just be defined once at the top-level. To fix this issue, you can use the following code:
ser.WriteStartObject(writer, o);
writer.WriteAttributeString("xmlns", "p", null, "some-reaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaally-long-namespace.com/");
ser.WriteObjectContent(writer, o);
ser.WriteEndObject(writer);
精彩评论