mardi 26 mai 2015

C# Update XML document within project

I'm wondering if its possible to update an XML document I have created for my tests which is stored within the main project folder and then copied to bin folder on each build. The project will be re-built before each test run.

The background to my query is, I'm looking to create a test where I can insert an xml document into a portal for uploading. The document has a unique identifier element within it which is the Id element, the document will only be accepted if the Id element is a new value which isn't already in the system.

What I'm looking to do on test initialize is to load the document using XmlDocument class and stripping it down to the Id element to store in a local int variable, then incrementing the int value by 1 to then pass back into the document and to update.

I can do this on one instance so the document stored in project and living in the bin folder has an Id = 1, then after first test run, the document stored in project stays with Id = 1 but the document living in the bin folder has it's Id incremented to Id = 2. This is all fine and well until the next time the project is re-built which would then have me in a state where documents in project/bin folder would revert to having an Id = 1.

Is there a way I can write to the xml document within the project?

Example of what I'm doing.

public override void Init()
{
     doc.Load("XmlDoc.xml");
     int Id = doc.ChildNodes.Item(0).ParentNode.FirstChild.InnerText;
     Id = Id + 1;
     doc.SelectSingleNode("//root/id").InnerText = Id;
     doc.Save("XmlDoc.xml");
}

Hopefully this makes some sort of sense :)

Aucun commentaire:

Enregistrer un commentaire