【问题标题】:Xml file with C# can't get the value使用 C# 的 xml 文件无法获取值
【发布时间】:2018-01-26 08:37:33
【问题描述】:

这是我的 xml 文件

<?xml version="1.0" encoding="ASCII"?>
<Vitals>
<Vendor>General Electric Healthcare</Vendor>
<Model>Pro/Procare</Model>
    <Result name="Mean_arterial_pressure">
        <Value>86</Value>
        <Units name="mmHg"></Units>
        <Time_stamp year="2008" month="11" day="18" hour="12" minute="33" second="14"></Time_stamp>
    </Result>
    <Result name="Systolic_blood_pressure">
        <Value>130</Value>
        <Units name="mmHg"></Units>
        <Time_stamp year="2008" month="11" day="18" hour="12" minute="33" second="14"></Time_stamp>
    </Result>
    <Result name="Diastolic_blood_pressure">
        <Value>67</Value>
        <Units name="mmHg"></Units>
        <Time_stamp year="2008" month="11" day="18" hour="12" minute="33" second="14"></Time_stamp>
    </Result>
    <Result name="Pulse">
        <Value>73</Value>
        <Units name="BPM"></Units>
        <Method>blood_pressure</Method>
        <Time_stamp year="2008" month="11" day="18" hour="12" minute="33" second="14"></Time_stamp>
  </Result>
</Vitals>

这是我的源代码,我遇到了如何获取结果名称和值的问题?

private void btnReadXml_Click(object sender, EventArgs e)
{

    Hashtable h = new Hashtable();
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load("C:\\dinamap.xml");


    XmlNodeList doc_results = xmlDoc.GetElementsByTagName("Vitals/Vendor/Model/Result[@name='Systolic_blood_pressure']");
    foreach (XmlNode pnode in doc_results)
    {
        foreach (XmlNode cnode in pnode.ChildNodes)
        {
            if (cnode.Name == "Value")
            {
                h.Add(pnode.Attributes["name"].InnerText + cnode.Name, cnode.Attributes["name"].InnerText);
            }
            else
            {
                h.Add(pnode.Attributes["name"].InnerText + "_" + cnode.Name, cnode.InnerText);
            }
        }      
    }
}

我可以知道我的代码有什么问题吗?总是无法获得价值。
我需要从 xml 文件中获取值。

【问题讨论】:

    标签: c# xml xpath


    【解决方案1】:

    在第 5 行,您的 xPath 指定 ModelVendor 的子代,而 Vendor 仅包含一个字符串 (&lt;Vendor&gt;General Electric Healthcare&lt;/Vendor&gt;)。

    此外,要使用 xPath 导航,我建议您使用 SelectNodes 函数。

    试试这个:

    XmlNodeList doc_results = xmlDoc.SelectNodes("/Vitals/Model/Result[@name='Systolic_blood_pressure']");
    

    【讨论】:

    • 如果我需要同时显示所有@name ="Systolic_blood_pressure"、"Diastolic_blood_pressure" 、name="Diastolic_blood_pressure" &" Pulse",以及他的
    【解决方案2】:

    如果您为此使用 LINQ to XML 会更容易。

    var doc = XDocument.Load("path/to/xml");
    
    var results =
        from result in doc.Descendants("Result")
        select new
        {
            Name = (string) result.Attribute("name"),
            Value = (string) result.Element("Value"),
            Units = (string) result.Element("Units").Attribute("name")
        };
    

    如果需要,您可以按名称过滤:

    var sysBp = results.Single(x => x.Name == "Systolic_blood_pressure");
    

    请参阅this fiddle 以获得工作演示。

    【讨论】:

      【解决方案3】:

      您当前正在尝试检索子节点的“名称”attribute,在您的值节点的情况下它实际上没有。如果您使用cnode.Value,您可以获得文本注释的内容。

      另外,Result不是Model 的子代,它不是Vendor 的子代。你要么想要正确地让它们成为孩子,要么改变你为doc_results设置的路径

      【讨论】:

        【解决方案4】:

        private void btnReadXml_Click(object sender, EventArgs e)
                {
        
                    var doc = XDocument.Load("C:\\dinamap.xml");
        
                    var results = from result in doc.Descendants("Result")
                                  select new
                                  {
                                      Name = (string)result.Attribute("name"),
                                      Value = (string)result.Element("Value"),
                                      Units = (string)result.Element("Units").Attribute("name")
                                  };
        
                    foreach (var result in results)
                    {
                        MessageBox.Show("{result.Name}: {result.Value} {result.Units}");
        
                    }
                 
                }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-04-11
          • 1970-01-01
          • 1970-01-01
          • 2021-05-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多