You must Sign In to post a response.
  • Category: .NET

    Re-arrange xml attribute value in C#

    i need to change the location of attribute xml value

    ex :
    from : <PCIInfo deviceID="1616" subDeviceID="1028" subVendorID="062B" vendorID="8086" />

    to :<PCIInfo deviceID="1616" vendorID="8086" subDeviceID="062B" subVendorID="1028" />
  • #769197
    Hi Ravindra,

    If the attribute you want to change doesn't exist or has been accidentally removed, then an exception will occur. I suggest you first create a new attribute and send it to a function like the following:

    private void SetAttrSafe(XmlNode node,params XmlAttribute[] attrList)
    {
    foreach (var attr in attrList)
    {
    if (node.Attributes[attr.Name] != null)
    {
    node.Attributes[attr.Name].Value = attr.Value;
    }
    else
    {
    node.Attributes.Append(attr);
    }
    }
    }
    Usage:

    XmlAttribute attr = dom.CreateAttribute("name");
    attr.Value = value;
    SetAttrSafe(node, attr);

    Thanks,
    bharati


Sign In to post your comments