- 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.
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>