how to remove an attribute from an System.Xml.XmlNode object in C#. The Code I tried did not work. It throw an exception “node to be removed is not valid child node”
foreach (XmlNode distribution
in responseXml.SelectNodes("/Distributions/Distribution/DistributionID"))
{
XmlAttribute attribute = null;
foreach (XmlAttribute attri in distribution.Attributes)
{
if (attri.Name == "GrossRevenue")
attribute = attri;
}
if (attribute != null)
distribution.ParentNode.RemoveChild(attribute);
}
XmlAttributes are not XmlNodes.
XmlNode.ChildNodesis of typeXmlNodeList, whileXmlNode.Attributesis of typeXmlAttributesCollection. To remove an attribute, you use theXmlAttributesCollection.Removeor.RemoveAtmethod. In your code: