如何在xmlstring中删除包含所有子节点的父节点

时间:2016-03-14 10:16:16

标签: c# xml

请帮我在xmlstring中重新标记整个标记 ReadRequests

我尝试如下删除所有子节点但显示ReadRequests。我也想删除这个标签。

请求:

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;

响应:

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

1 个答案:

答案 0 :(得分:0)

请改为尝试:

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;