Note that there are some explanatory texts on larger screens.

plurals
  1. POSerializing Object to XML and removing unwanted text from XML
    primarykey
    data
    text
    <p>I have an object that I want to serialize to XML and I am using the following code to carry this out:</p> <pre><code> 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(); } } </code></pre> <p>and when I do, I get the following XML:</p> <pre><code>&lt;TestRequestPOCO xmlns=\"http://schemas.datacontract.org/2004/07/GPTR.Model.POCOs\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"&gt; &lt;AdditionalInformation&gt;Additional Information&lt;/AdditionalInformation&gt; &lt;AddressLine1&gt;6 MOUNT PLEASANT ROAD&lt;/AddressLine1&gt; &lt;AddressLine2&gt;LEEDS&lt;/AddressLine2&gt; &lt;AddressLine3 i:nil=\"true\"/&gt; &lt;AddressLine4 i:nil=\"true\"/&gt; &lt;AntibioticTherapy i:nil=\"true\"/&gt; &lt;ClinicalInformation&gt;Clinical Information&lt;/ClinicalInformation&gt; &lt;ClinicalInformationXml i:nil=\"true\"/&gt; &lt;Clinician&gt;Dr NM BRYAN&lt;/Clinician&gt; &lt;ClinicianCode&gt;4203845&lt;/ClinicianCode&gt; &lt;ClinicianShortCode :nil=\"true\"/&gt; &lt;Destination&gt;1&lt;/Destination&gt; &lt;Dob&gt;1992-02-29T00:00:00&lt;/Dob&gt; &lt;ExpectedDate&gt;2011-10-06T10:22:57.096+01:00&lt;/ExpectedDate&gt; &lt;FirstName&gt;ALISON&lt;/FirstName&gt; &lt;GenerateOrder&gt;true&lt;/GenerateOrder&gt; &lt;HospitalNumber i:nil=\"true\"/&gt; &lt;IsFasting&gt;false&lt;/IsFasting&gt; &lt;IsPrivatePatient&gt;false&lt;/IsPrivatePatient&gt; &lt;IsSensitive&gt;false&lt;/IsSensitive&gt; &lt;IsUrgent&gt;false&lt;/IsUrgent&gt; &lt;Items&gt; &lt;RequestDataItem&gt; &lt;AdditionalInfo i:nil=\"true\"/&gt; &lt;Container i:nil=\"true\"/&gt; &lt;Description&gt;Ferritin [Serum]&lt;/Description&gt; &lt;LIMSDeptName&gt;CHM&lt;/LIMSDeptName&gt; &lt;ProfileNumber&gt;1293&lt;/ProfileNumber&gt; &lt;QualifierCode i:nil=\"true\"/&gt; &lt;SiteCode i:nil=\"true\"/&gt; &lt;SpecimenType&gt;Serum&lt;/SpecimenType&gt; &lt;UniqueTest&gt;False&lt;/UniqueTest&gt; &lt;/RequestDataItem&gt; &lt;/Items&gt; &lt;Location&gt;0&lt;/Location&gt; &lt;LocationName&gt;The INPS Practice&lt;/LocationName&gt; &lt;LocationShortCode&gt;W97046&lt;/LocationShortCode&gt; &lt;LocationTelephone&gt;01792602898&lt;/LocationTelephone&gt; &lt;MiddleName i:nil=\"true\"/&gt; &lt;NhsNumber&gt;5240022631&lt;/NhsNumber&gt; &lt;OrgCode&gt;RRS&lt;/OrgCode&gt; &lt;Placer&gt;Dr Sarah Saturn&lt;/Placer&gt; &lt;PostCode&gt;CF31 5EP&lt;/PostCode&gt; &lt;Sex&gt;Male&lt;/Sex&gt; &lt;Source&gt;GPTR&lt;/Source&gt; &lt;SurName&gt;WILLIAMS&lt;/SurName&gt; &lt;TelephoneNumber&gt;01792776776&lt;/TelephoneNumber&gt; &lt;/TestRequestPOCO&gt;" </code></pre> <p>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:</p> <pre><code>&lt;TestRequestPOCO&gt; &lt;AdditionalInformation&gt;Additional Information&lt;/AdditionalInformation&gt; &lt;AddressLine1&gt;6 MOUNT PLEASANT ROAD&lt;/AddressLine1&gt; &lt;AddressLine2&gt;LEEDS&lt;/AddressLine2&gt; &lt;AddressLine3/&gt; &lt;AddressLine4/&gt; &lt;AntibioticTherapy&gt; &lt;ClinicalInformation&gt;Clinical Information&lt;/ClinicalInformation&gt; &lt;ClinicalInformationXml/&gt; &lt;Clinician&gt;Dr NM BRYAN&lt;/Clinician&gt; &lt;ClinicianCode&gt;4203845&lt;/ClinicianCode&gt; &lt;ClinicianShortCode/&gt; &lt;Destination&gt;1&lt;/Destination&gt; &lt;Dob&gt;1992-02-29T00:00:00&lt;/Dob&gt; &lt;ExpectedDate&gt;2011-10-06T10:22:57.096+01:00&lt;/ExpectedDate&gt; &lt;FirstName&gt;ALISON&lt;/FirstName&gt; &lt;GenerateOrder&gt;true&lt;/GenerateOrder&gt; &lt;HospitalNumber/&gt; &lt;IsFasting&gt;false&lt;/IsFasting&gt; &lt;IsPrivatePatient&gt;false&lt;/IsPrivatePatient&gt; &lt;IsSensitive&gt;false&lt;/IsSensitive&gt; &lt;IsUrgent&gt;false&lt;/IsUrgent&gt; &lt;Items&gt; &lt;RequestDataItem&gt; &lt;AdditionalInfo/&gt; &lt;Container/&gt; &lt;Description&gt;Ferritin [Serum]&lt;/Description&gt; &lt;LIMSDeptName&gt;CHM&lt;/LIMSDeptName&gt; &lt;ProfileNumber&gt;1293&lt;/ProfileNumber&gt; &lt;QualifierCode/&gt; &lt;SiteCode/&gt; &lt;SpecimenType&gt;Serum&lt;/SpecimenType&gt; &lt;UniqueTest&gt;False&lt;/UniqueTest&gt; &lt;/RequestDataItem&gt; &lt;/Items&gt; &lt;Location&gt;0&lt;/Location&gt; &lt;LocationName&gt;The INPS Practice&lt;/LocationName&gt; &lt;LocationShortCode&gt;W97046&lt;/LocationShortCode&gt; &lt;LocationTelephone&gt;01792602898&lt;/LocationTelephone&gt; &lt;MiddleName /&gt; &lt;NhsNumber&gt;5240022631&lt;/NhsNumber&gt; &lt;OrgCode&gt;RRS&lt;/OrgCode&gt; &lt;Placer&gt;Dr Sarah Saturn&lt;/Placer&gt; &lt;PostCode&gt;CF31 5EP&lt;/PostCode&gt; &lt;Sex&gt;Male&lt;/Sex&gt; &lt;Source&gt;GPTR&lt;/Source&gt; &lt;SurName&gt;WILLIAMS&lt;/SurName&gt; &lt;TelephoneNumber&gt;01792776776&lt;/TelephoneNumber&gt; &lt;/TestRequestPOCO&gt;" </code></pre> <p>Does anyone have any idea how I can strip these out and come up with a string of XML like above.</p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload