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);
}
The removeChild() method removes a specified node. The removeAttribute() method removes a specified attribute.
XmlAttributes are not XmlNodes. XmlNode.ChildNodes
is of type XmlNodeList
, while XmlNode.Attributes
is of type XmlAttributesCollection
. To remove an attribute, you use the XmlAttributesCollection.Remove
or .RemoveAt
method. In your code:
distribution.ParentNode.Attributes.Remove(attribute);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With