Note that there are some explanatory texts on larger screens.

plurals
  1. POChange Value of the last Attribute in an XML c#
    text
    copied!<p>I was working on a bunch of XMLs that all share an attribute that contains the string "name" in them. The following code selects the attribute with string "name" in it and assign a new value to it.</p> <pre><code> public void updateXmlFile(string strFileName) { try { //Load the Document XmlDocument doc = new XmlDocument(); doc.Load(strFileName); //Set the changed Value string newValue = GetUniqueKey(); //Select all nodes in the XML then choose from them //the first node that contain string "name" in it XmlNodeList list = doc.SelectNodes("//@*"); XmlNode filteredNode = list.Cast&lt;XmlNode&gt;() .First(item =&gt; item.Name.ToLower().Contains("name")); //Assign the newValue to the value of the node filteredNode.Value = newValue; doc.Save(strFileName); } catch (XmlException xex) { Console.WriteLine(xex); } } </code></pre> <p>Now a new XMLs were added that dosen't have the string "name" in them, so instead of modifying the attribute with string "name" in it I decided to simply modify the last attribute no matter what it was (not the first)</p> <p>Can anybody tell me how to do that?</p> <p><strong>EDIT</strong></p> <p>Here is an example of my XML</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;CO_CallSignLists Version="24" ModDttm="2010-09-13T06:45:38.873" ModUser="EUADEV\SARE100" ModuleOwner="EUADEVS06\SS2008" CreateDttm="2009-11-05T10:19:31.583" CreateUser="EUADEV\A003893"&gt; &lt;CoCallSignLists DataclassId="E3FC5E2D-FE84-492D-AD94-3ACCED870714" EntityId="E3FC5E2D-FE84-492D-AD94-3ACCED870714" MissionID="4CF71AB2-0D92-DE11-B5D1-000C46F3773D" BroadcastType="S" DeputyInSpecialList="1" SunSpots="1537634cb70c6d80"&gt; &lt;CoCallSigns EntityId="DEBF1DDB-3C92-DE11-A280-000C46F377C4" CmdID="C45F3EF1-1292-DE11-B5D1-000C46F3773D" ModuleID="6CB497F3-AD63-43F1-ACAE-2C5C3B1D7F61" ListType="HS" Name="Reda Sabassi" Broadcast="INTO" PhysicalAddress="37" IsGS="1" HCId="0" CommonGeoPos="1" GeoLat="0.0000000" GeoLong="0.0000000"&gt; &lt;CoRadios EntityId="E1BF1DDB-3C92-DE11-A280-000C46F377C4" RadioType="HF" /&gt; &lt;/CoCallSigns&gt; &lt;/CoCallSignLists&gt; &lt;/CO_CallSignLists&gt; </code></pre> <p>@Alex: You notice that the "SunSpots" attribute (last attribute in the first child element) is successfully changed. But now when I wanna load the XML back into the DB it gives me an error</p> <p>Here is the modified code</p> <pre><code> public void updateXmlFile(string strFileName) { try { XDocument doc = XDocument.Load(strFileName); XAttribute l_attr_1 = (doc.Elements().First().Elements().First().Attributes().Last()); l_attr_1.Value = GetUniqueKey(); Console.WriteLine("Name: {0} Value:{1}", l_attr_1.Name, l_attr_1.Value); doc.Save(strFileName); } catch (XmlException xex) { Console.WriteLine(xex); } } </code></pre> <p>I was thinking of making an if statment which checks if the XML has an attribute that contains string "name" in it (since most of my XMLs has an attribute that contains name in them) if it does then change the attribute's value if not look for the last attribute and change it.. not the best solution but just throwing it out there</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload