2

I have a bunch of XPathExpressions that I used to read an XML file. I now need go the other way. (Generate an XML file based on the values I have.)

Here is an example to illustrate. Say I have a bunch of code like this:

XPathExpression hl7Expr1 = navigator.Compile("/ORM_O01/MSH/MSH.6/HD.1");
var hl7Expr2 = navigator.Compile("/ORM_O01/ORM_O01.PATIENT/PID/PID.18/CX.1");
var hl7Expr3 = navigator.Compile("/ORM_O01/ORM_O01.PATIENT/ORM_O01.PATIENT_VISIT/PV1/PV1.19/CX.1");
var hl7Expr4 = navigator.Compile("/ORM_O01/ORM_O01.PATIENT/PID/PID.3[1]/CX.1");
var hl7Expr5 = navigator.Compile("/ORM_O01/ORM_O01.PATIENT/PID/PID.5[1]/XPN.1/FN.1");
var hl7Expr6 = navigator.Compile("/ORM_O01/ORM_O01.PATIENT/PID/PID.5[1]/XPN.2");

string hl7Value1 = "SomeValue1";
string hl7Value2 = "SomeValue2";
string hl7Value3 = "SomeValue3";
string hl7Value4 = "SomeValue4";
string hl7Value5 = "SomeValue5";
string hl7Value6 = "SomeValue6";

Is there a way to take the hl7Expr XPathExpressions and generate an XML file with the corresponding hl7Value string in it?

Or maybe just use the actual path string to do the generation (instead of using the XPathExpression object)?

Note: I saw this question: Create XML Nodes based on XPath? but the answer does not allow for [1] references like I have on hl7Expr4.

Community
  • 1
  • 1
Vaccano
  • 78,325
  • 149
  • 468
  • 850

1 Answers1

2

I found this answer: https://stackoverflow.com/a/3465832/16241

And I was able to modify the main method to convert the [1] to attributes (like this):

public static XmlNode CreateXPath(XmlDocument doc, string xpath)
{
    XmlNode node = doc;
    foreach (string part in xpath.Substring(1).Split('/'))
    {
        XmlNodeList nodes = node.SelectNodes(part);
        if (nodes.Count > 1) throw new ApplicationException("Xpath '" + xpath + "' was not found multiple times!");
        else if (nodes.Count == 1) { node = nodes[0]; continue; }

        if (part.StartsWith("@"))
        {
            var anode = doc.CreateAttribute(part.Substring(1));
            node.Attributes.Append(anode);
            node = anode;
        }
        else
        {
            string elName, attrib = null;
            if (part.Contains("["))
            {
                part.SplitOnce("[", out elName, out attrib);
                if (!attrib.EndsWith("]")) throw new ApplicationException("Unsupported XPath (missing ]): " + part);
                attrib = attrib.Substring(0, attrib.Length - 1);
            }
            else elName = part;

            XmlNode next = doc.CreateElement(elName);
            node.AppendChild(next);
            node = next;

            if (attrib != null)
            {
                if (!attrib.StartsWith("@"))
                {
                    attrib = " Id='" + attrib + "'";
                }
                string name, value;
                attrib.Substring(1).SplitOnce("='", out name, out value);
                if (string.IsNullOrEmpty(value) || !value.EndsWith("'")) throw new ApplicationException("Unsupported XPath attrib: " + part);
                value = value.Substring(0, value.Length - 1);
                var anode = doc.CreateAttribute(name);
                anode.Value = value;
                node.Attributes.Append(anode);
            }
        }
    }
    return node;
}
Community
  • 1
  • 1
Vaccano
  • 78,325
  • 149
  • 468
  • 850