Reading XML file, updating contents and saving it again.

Status
Not open for further replies.

TrevorC

Silver Partner
Advanced Certified
Joined
Nov 4, 2019
Messages
9
Reaction score
0
I'm trying to create a call flow that can check the status of 8 different offices and was hoping I could use an XML file do to this, as I can then give the users a URL to just view the XML file in their web browser to quickly and easily see the status of the different offices.

I can read and check the current office status, but I'm having trouble updating the status and writing it back to the XML file.
This is the first time I've used an XML file, in any programming language, so I could be missing something obvious.

I started from the example "Creating a Phone Support Portal with the 3CX Call Flow Designer – Part 3"
https://www.3cx.com/docs/cfd-creating-phone-support-portal-3/

My code uses the Read / Write to file component to read the contents of the XML file into a string (the same as the sample)

To check the status of an office, I pass that string to my CheckOfficeStatus function as "fileContent" along with the office ID and a status (to compare to). I've not shown this code as it's just a modified version of the ValidateXML and is working correctly.


The sample code uses xmlDocument.LoadXML(fileContent) to load that string (containing the XML data) and then check the values of the nodes, and I can follow that.

To update the office status (from closed to open, or open to closed) I've created a second c# file (shown below)
I pass this the office ID (1, 2, 3 etc), office name (probably not needed) and the status I'd like to set that office to (either open or closed)

In the function I then loop through the xmlNodes to find the office (by ID), much like the validate example, and then try to update the status, which is where I'm stuck.

Firstly, I'm not sure if the line
xmlNode.Attributes["status"].Value = status;
is valid (ie: update the status of the office that matches the desired ID to the new status).
Will this update the string, I suspect not as if I return the string, the value is unchanged.

I'm thinking I need to somehow write that xmlDocument back to a string, to return from the function.

If I return my string "fileContent", the contents aren't changed or updated when I write the XML file.
If I try to return xmlDocument.OuterXML; I get an error when I upload to 3CX that there is no definition for OuterXML, so obviously that's the wrong syntax.


Any help or ideas are very much appreciated.


using System.Xml; public class Updater { public string UpdateStatusXML(string fileContent, string id, string name, string status) { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.LoadXml(fileContent); XmlNodeList officeElementList = xmlDocument.GetElementsByTagName("office"); foreach (XmlNode xmlNode in officeElementList) { string officeID = xmlNode.Attributes["id"].Value; if (id == officeID) { xmlNode.Attributes["status"].Value = status; } } return fileContent; // This returns the original (unmodified XML string) // return xmlDocument.OuterXML; // This failes when the call flow is uploaded as 3CX // Error CS1061: 'XmlDocument' does not contain a definition for 'OuterXML' and no accessible extension method 'OuterXML' // accepting a first argument of type 'XmlDocument' could be found (are you missing a using directive or an assembly reference?) } }


Sample XML:​

<offices> <office id="1" name="Office 1" status="open"/> <office id="2" name="Office 2" status="closed"/> <office id="3" name="Office 3" status="open"/> <office id="4" name="Office 4" status="closed"/> <office id="5" name="Office 5" status="open"/> <office id="6" name="Office 6" status="open"/> <office id="7" name="Office 7" status="open"/> <office id="8" name="Office 8" status="open"/> </offices>
 
Hi Ernesto

Thanks for the reply. I've had a bit of a crazy week so haven't had a chance post back that I was able to work-around the issue.

I know my question was more of a general programming issue, but I wasn't able to get the sample code I'd come up with to work under 3CX CFD and I wasn't sure if it was a limitation of the C# libraries used by 3CX CFD.

Unfortunately, the link you sent me, and most that I could find, give examples of converting a text string, that as read from a file, to an XML document, that can then be searched.
What I was trying to do was to modify the XML document, then convert it back to a string that 3CX could then write back to the file.

I found some samples that suggested this can be done by using OuterXML on the XML document, but couldn't get that to work as it said it wasn't found in the library.

The workaround I figured out was to just use the C# replace one string with another.

So to close an office I just replaced:
name="Office 1" status="open"
with
name="Office 1" status="closed"

or visa-versa to open the office.
Obviously as my XML contains multiple offices, I have to include the office name, to just change the appropriate office status to open / closed as needed.

It's a workaround, of sorts, but it does what I need, so I'm happy to live with it for now as I'm hoping we can simplify this all when update 7 is out by using groups for each office and setting the status of the group instead.

Either way, this is working for now so I'm happy, and more importantly, so is our customer.
 
Good that you found a solution. Actually, this will be more performant than parsing the entire XML, just to change a node and serialize it again. You just need to be sure that you don't add an extra space before status for example. But it should work fine.
 
JSON would be easier to deal with than XML
 
I'm hoping we can simplify this all when update 7 is out by using groups for each office and setting the status of the group instead.
This is already out since U6.
 
Status
Not open for further replies.

Members Online Now

Forum statistics

Threads
111,842
Messages
589,320
Members
164,677
Latest member
skander