2

I wanna add a Test (common) attribute to all my XML files. So that I could use it as a common attribute when I wanna test them.

I tried CreateAttribute but Linq dosen't recognize it

I tried "xElement.Add(new XAttribute("Test", value));" but it also didn't work Any Suggestions?

Thanks

Here for example is a code

    public void updateXmlFile(string strFileName)
    {
        XDocument oXDoc = XDocument.Load(strFileName);
        XElement oDcElement = oXDoc.Root.FirstNode as XElement;

        //Generate a Unique String to replace the original attribute value
        string newValue = GetUniqueKey();

        //oDcElement.Add(new XAttribute("Test", newValue)); /*NullReferenceException*/

        oDcElement.Attribute("Remark").Value = newValue; //This changes only the Remark Attribute
        oXDoc.Save(strFileName);                         //which isn't available in all XMLs

    }

I wanna add an additional, common value to the XMLs I pass through this method and give it a random value

My goal is to be able to make changes on an XML then compare it against the original copy in another folder

1
  • You seem to be on the right lines, please can you paste the full source? Commented Aug 25, 2010 at 10:47

1 Answer 1

8

Use SetAttribute:

oDcElement.SetAttributeValue("Test", newValue);
Sign up to request clarification or add additional context in comments.

2 Comments

I figured I can't do it since I'm retrieving Data from the DB and if I wanna add an attribute I have to add an extra coloumn to the DB.
Your original question says nothing about a DB. It's just adding a new attribute to a node of an XML file loaded from the filesystem. I don't understand what your comment means.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.