【问题标题】:Build XML file from XPathExpressions从 XPathExpressions 构建 XML 文件
【发布时间】:2011-12-07 08:11:01
【问题描述】:

我有一堆用来读取 XML 文件的 XPathExpressions。我现在需要走另一条路。 (根据我拥有的值生成一个 XML 文件。)

这里有一个例子来说明。假设我有一堆这样的代码:

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";

有没有办法获取 hl7Expr XPathExpressions 并生成一个包含相应 hl7Value 字符串的 XML 文件?

或者也许只是使用实际的路径字符串来进行生成(而不是使用 XPathExpression 对象)?

注意:我看到了这个问题:Create XML Nodes based on XPath?,但答案不允许像我在 hl7Expr4 上的 [1] 引用。

【问题讨论】:

    标签: c# xml xpath


    【解决方案1】:

    我找到了这个答案:https://stackoverflow.com/a/3465832/16241

    并且我能够修改将 [1] 转换为属性的 main 方法(像这样):

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-22
      相关资源
      最近更新 更多