简体   繁体   中英

how to remove Parent node with all children in xmlstring

Please help me to reomove entire tag ReadRequests in a xmlstring .

I tried as below removed all child nodes but showing ReadRequests. i want to remove this tag also.

Request:

string postData = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<OTA_ReadRQ Version=""5.001="""" xmlns=""http:=""//www.opentravel.org/OTA/2003/05"">
<POS>
  <Source>
    <RequestorID MessagePassword=""$yncr00m3="""" ID=""sync-rooms="""" Type=""CHM=""""/>
  </Source>
</POS>
<UniqueID ID=""0002757404="""" ID_Context=""Book="""" />
<ReadRequests>
  <HotelReadRequest HotelCode=""00023642="""">
    <SelectionCriteria  Start=""2016-03-11T00:00:00="""" End=""2016-03-11T23:00:00=""""/>
  </HotelReadRequest>
</ReadRequests>
</OTA_ReadRQ>";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(postData);
xmlDoc.DocumentElement["ReadRequests"].RemoveAll();
string  Getdata = xmlDoc.InnerXml;

Response:

<?xml version="1.0" encoding="UTF-8"?>
<OTA_ReadRQ Version="5.001" xmlns="http://www.opentravel.org/OTA/2003/05">
  <POS>
    <Source>
      <RequestorID MessagePassword="$yncr00m3" ID="sync-rooms" Type="CHM" />
    </Source>
  </POS>
  <UniqueID ID="0002757404" ID_Context="Book" />
  <ReadRequests></ReadRequests>
</OTA_ReadRQ>

Try this instead:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(postData);
//xmlDoc.DocumentElement["ReadRequests"].RemoveAll();
XmlNode node = xmlDoc.DocumentElement.GetElementsByTagName("ReadRequests")[0];
if (node != null)
{
    xmlDoc.DocumentElement.RemoveChild(node);
}
string Getdata = xmlDoc.InnerXml;

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