【问题标题】:How to get the child element values of a xml parent node in c# asp.net(without using linq)如何在c#asp.net中获取xml父节点的子元素值(不使用linq)
【发布时间】:2014-02-17 13:09:12
【问题描述】:

我有一个带有嵌套元素的 xml 文件。我需要加载 xml 文件并将特定子元素的值保存到 sql 数据库中。

xml 文件:

    <section id="A00-A09">
          <desc>Intestinal infectious diseases (A00-A09)</desc>
          <diag>
            <name>A00</name>
            <desc>Cholera</desc>
            <diag>
              <name>A00.0</name>
              <desc>Cholera due to Vibrio cholerae 01, biovar cholerae</desc>
              <inclusionTerm>
                <note>Classical cholera</note>
              </inclusionTerm>
            </diag>
            <diag>
              <name>A00.1</name>
              <desc>Cholera due to Vibrio cholerae 01, biovar eltor</desc>
              <inclusionTerm>
                <note>Cholera eltor</note>
              </inclusionTerm>
            </diag>
            <diag>
              <name>A00.9</name>
              <desc>Cholera, unspecified</desc>
            </diag>
          </diag>
    </section>
     <section id="A15-A19">
          <desc>Tuberculosis (A15-A19)</desc>
          <includes>
            <note>infections due to Mycobacterium tuberculosis and Mycobacterium bovis</note>
          </includes>
        <diag>
            <name>A15</name>
            <desc>Respiratory tuberculosis</desc>
            <diag>
              <name>A15.0</name>
              <desc>Tuberculosis of lung</desc>
              <inclusionTerm>
                <note>Tuberculous bronchiectasis</note>
                <note>Tuberculous fibrosis of lung</note>
                <note>Tuberculous pneumonia</note>
                <note>Tuberculous pneumothorax</note>
              </inclusionTerm>
            </diag>
            <diag>
              <name>A15.4</name>
              <desc>Tuberculosis of intrathoracic lymph nodes</desc>
              <inclusionTerm>
                <note>Tuberculosis of hilar lymph nodes</note>
                <note>Tuberculosis of mediastinal lymph nodes</note>
                <note>Tuberculosis of tracheobronchial lymph nodes</note>
              </inclusionTerm>
              <excludes1>
                <note>tuberculosis s`enter code here`pecified as primary (A15.7)</note>
              </excludes1>
            </diag>
    </diag>
    </section>

我已加载 xml 但无法从子元素中获取值,因为它具有相同的标记名称。有没有人可以帮我解决这个问题

【问题讨论】:

  • 您想在没有 linq 的情况下执行此操作的任何具体原因?我认为如果没有 linq,这项任务会更加困难,特别是因为存在同名的嵌套元素。请发布一些您尝试过的相关代码,即使它失败了。
  • 好的,你能给我一个linq中的解决方案吗
  • 您的 xml 文件在发布时无效(它有多个根元素)。那是实际的格式吗? 是的,这就是实际的格式。那么在这种情况下我们不能使用 linq,因为文件不能作为XDocument 对象加载。否则,如果 xml 文件有一个根且有效,我们就有机会使用 linq,而这种情况下的问题是您想从哪个特定元素中获取值?
  • xml 文件以根 开头。
    标签是子元素。我需要获取
    Desription() 和
    值的子标签
  • 的子标签是什么意思?目前还不清楚。请编辑问题并清楚地描述您希望从该示例 xml 中获得的输出/值。

标签: xml c#-4.0 c#-3.0 c#-2.0


【解决方案1】:

以下控制台应用程序示例演示了一种获取&lt;desc&gt; 元素的方法,该元素是&lt;section&gt; 元素的直接子元素,还可以在每个&lt;diag&gt; 元素下获取&lt;name&gt;&lt;desc&gt;。我正在使用来自System.Xml.LinqXDocument。但不使用 linq 查询语法,因为在这种情况下没有必要,而且您似乎不喜欢它:

//load xml string
var doc = XDocument.Parse(xml);
//or use XDocument.Load("path_to_xml_file.xml"); to load from file

//get all <section> element
var sections = doc.Root.Elements("section");
//get <desc> and all <diag> under each <section>
foreach (var section in sections)
{
    var desc = (string)section.Element("desc");
    Console.WriteLine(String.Format("new Section. Desc: {0}", desc));
    var diags = section.Descendants("diag");
    foreach (var diag in diags)
    {
        var name = (string)diag.Element("name");
        var desc2 = (string)diag.Element("desc");
        Console.WriteLine(String.Format("name: {0}, desc: {1}", name, desc2));
    }
}

注意Descendants() 用于获取当前元素的任何子元素,Element() 用于仅获取当前元素的直接子元素。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-04
    相关资源
    最近更新 更多