0

I have a piece of XML that is prefixed with an "a:". I incorporated a regular expression removing the undeclared prefix "a:" from the xml in order for me to easily serialise it into an object, however when doing so i seem to remove the closing tag for "ManagedInstallModuleQuantity".

Can anyone help

var finalXml = Regex.Replace(formatedXml, @"<(/?)\w+:(\w+/?) ?(\w+:\w+.*)?>", "<$1$2>");

XML Before removal:

<GetAppointmentDetailsResult>
<Status>
    <Errors/>
    <HasErrors>false</HasErrors>
</Status>
<a:Action>Provide</a:Action>
<a:AddressKey>A00008222007</a:AddressKey>
<a:AppointmentType>Standard</a:AppointmentType>
<a:EarliestAppointmentDetails>
    <a:AppointmentDate>2019-10-29T00:00:00</a:AppointmentDate>
    <a:AppointmentTimeSlot>AM</a:AppointmentTimeSlot>
</a:EarliestAppointmentDetails>
<a:HomeWiringSolutionOption>NotSet</a:HomeWiringSolutionOption>
<a:ManagedInstallModuleQuantity i:nil=\"true\" />
<a:NumberOfLines>1</a:NumberOfLines>
<a:PCPOnlyInstall>No</a:PCPOnlyInstall>
<a:ReservedAppointmentDetails>
    <a:AppointmentDate>2019-11-25T00:00:00</a:AppointmentDate>
    <a:AppointmentTimeSlot>AM</a:AppointmentTimeSlot>
</a:ReservedAppointmentDetails>
<a:ServiceLevel>CareLevel2</a:ServiceLevel>
<a:ServiceType>FTTCSim2</a:ServiceType>
<a:Technology>VDSL</a:Technology>
</GetAppointmentDetailsResult>

XML after regular expression implementation:

<GetAppointmentDetailsResult>
<Status>
    <Errors/>
        <HasErrors>false</HasErrors>
</Status>
<Action>Provide</Action>
<AddressKey>A00008222007</AddressKey>
<AppointmentType>Standard</AppointmentType>
<EarliestAppointmentDetails>
    <AppointmentDate>2019-10-29T00:00:00</AppointmentDate>
    <AppointmentTimeSlot>AM</AppointmentTimeSlot>
</EarliestAppointmentDetails>
<HomeWiringSolutionOption>NotSet</HomeWiringSolutionOption>
<ManagedInstallModuleQuantity>
ashley g
  • 857
  • 3
  • 11
  • 21
  • Have a look at - https://stackoverflow.com/questions/987135 – Rand Random Oct 22 '19 at 09:25
  • That's a schema namespace, not a prefix. Those are perfectly valid XML. You don't need to remove it. If you want to remove the namespace, *don't* use a regex after the fact - specify the namespace URL as the default schema instead, with `xmlns="..."`. – Panagiotis Kanavos Oct 22 '19 at 09:27
  • Than maybe this - https://stackoverflow.com/questions/8634369 – Rand Random Oct 22 '19 at 09:28
  • What is the *full* XML document? What you posted here isn't a full document and suggests that *removing* the namespace may not be possible - `Status` clearly belongs to a *different* namespace. You may end up with *invalid* XML if you remove the namespaces. What are you trying to do? What is the original problem? – Panagiotis Kanavos Oct 22 '19 at 09:29
  • It is not recommended to remove the prefix. The prefix is designed to work with a schema that can validate correct data is inserted into the xml and removing prefix removes the error checking. Removing the prefix is like removing the compiler errors in c# and just getting one message when compiling fails : ERROR with no line number and no reason for the error. – jdweng Oct 22 '19 at 09:32
  • I am trying to serialise the XML into an object and upon doing so the serializer complains about the namespaces, therefore when removing them the XML is serialised into an object without any problems. Note the xml you are seeing is data that has already been retrieved from a post request. I just want to manipulate this retrieved data and create an object from it. – ashley g Oct 22 '19 at 09:57

0 Answers0