C# XML 读取 foreach 总是第一个值

我的 XML


<?xml version="1.0" encoding="UTF-8"?>

 <teklif>

 <bilgiler>

<firma>Firma Adı</firma>

<aciklama>Açıklama</aciklama>

<isim>Ad Soyad</isim>

<telefon>Telefon</telefon>

<eposta>E Posta</eposta>

<urunler>

  <urun>

    <resimDosyasi>Dosya Seçilmedi</resimDosyasi>

    <aciklama>Ürün Açıklaması</aciklama>

    <birim>3,00</birim>

    <miktar>1</miktar>

    <toplam>0,00</toplam>

  </urun>

  <urun>

    <resimDosyasi>Dosya Seçilmedi</resimDosyasi>

    <aciklama>Ürün Açıklaması</aciklama>

    <birim>5,00</birim>

    <miktar>1</miktar>

    <toplam>0,00</toplam>

  </urun>

  <urun>

    <resimDosyasi>Dosya Seçilmedi</resimDosyasi>

    <aciklama>aas</aciklama>

    <birim>2,00</birim>

    <miktar>1</miktar>

    <toplam>0,00</toplam>

  </urun>

</urunler>

以及读取 XML 文件的 My Function


        XmlDocument doc = new XmlDocument();

        doc.Load(filename);

        XmlNodeList xmllist = doc.SelectNodes("/teklif/bilgiler/urunler");

        foreach(XmlNode nod in xmllist)

        {

            foreach(XmlNode childNode in nod.ChildNodes)

            {

                // her ürünün childnode oldu


                    if(childNode.Name == "#text")

                    {

                    } else

                    {

                    var urun_resim = childNode.SelectSingleNode("//resimDosyasi").InnerText;

                    var urun_aciklama = childNode.SelectSingleNode("//aciklama").InnerText;

                    var urun_birim = childNode.SelectSingleNode("//birim").InnerText;

                    MessageBox.Show(urun_birim);

                    var urun_miktar = childNode.SelectSingleNode("//miktar").InnerText;

                    var urun_toplam = childNode.SelectSingleNode("//toplam").InnerText;

                    var urun = new Urun(urun_resim, urun_birim, urun_miktar, urun_aciklama);

                    lw_urunler.Items.Add(urun);


                    }

            }

        }

问题是当我在 foreach 循环中向 //birim 发送消息框时,它总是写入第一个 - 3,00((3 次)。正如您在 XML 中看到的,第一个是 3,00,第二个是 5,00第三个是 2,00 但它总是写第一个。我查了很多,但我看不出问题。


慕娘9325324
浏览 513回答 3
3回答

炎炎设计

尝试不带//,例如childNode.SelectSingleNode("birim")。两个正斜杠表示 XML 文档的根,我的猜测是birim每次总是找到从根开始的第一个节点。

当年话下

使用 xml linq :using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml;using System.Xml.Linq;namespace ConsoleApplication52{&nbsp; &nbsp; class Program&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; const string FILENAME = @"c:\temp\test.xml";&nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XDocument doc = XDocument.Load(FILENAME);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<urun> uruns = doc.Descendants("urun").Select(x => new urun() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resimDosyasi = (string)x.Element("resimDosyasi"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; aciklama = (string)x.Element("aciklama"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; birim = (string)x.Element("birim"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; miktar = (int)x.Element("miktar"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toplam = (string)x.Element("toplam")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }).ToList();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public class urun&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public string resimDosyasi { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string aciklama { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string birim { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public int miktar { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string toplam { get; set; }&nbsp; &nbsp; }}

米脂

// 表示选择节点,无论它们在当前上下文中的任何位置。当前上下文默认为根。要将结果限制为当前子节点下的结果,请遵循以下模式(只需添加一个点):var&nbsp;urun_resim&nbsp;=&nbsp;childNode.SelectSingleNode(".//resimDosyasi").InnerText;
打开App,查看更多内容
随时随地看视频慕课网APP