C#读取每个值的XML总是第一个值

C#读取每个值的XML总是第一个值,c#,.net,xml,C#,.net,Xml,我的XML 问题是,当我在foreach循环中调用//birim消息框时,它总是写入第一个-3,00((3次)。正如您在XML中看到的,第一个是3,00,第二个是5,00,第三个是2,00,但它总是写入第一个。我检查了很多,但我看不出问题。尝试不使用/,例如childNode。选择Singlenode(“birim”)。两个前斜杠表示XML文档的根,我猜每次都是从根开始查找第一个birim节点。使用XML linq: XmlDocument doc = new XmlDocume

我的XML


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

尝试不使用
/
,例如
childNode。选择Singlenode(“birim”)
。两个前斜杠表示XML文档的根,我猜每次都是从根开始查找第一个
birim
节点。

使用XML linq:

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

                    }
            }
        }
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序52
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(文件名);
列出urun=doc.substands(“urun”)。选择(x=>newurun(){
resimDosyasi=(字符串)x.Element(“resimDosyasi”),
aciklama=(字符串)x.Element(“aciklama”),
birim=(字符串)x.Element(“birim”),
miktar=(int)x.Element(“miktar”),
toplam=(字符串)x.Element(“toplam”)
}).ToList();
}
}
公共类乌伦
{
公共字符串resimDosyasi{get;set;}
公共字符串aciklama{get;set;}
公共字符串birim{get;set;}
公共整数miktar{get;set;}
公共字符串toplam{get;set;}
}
}

另一种提高代码可读性和维护性的方法是将XML模式映射到对象模型中

这样,您可以按如下方式轻松加载XML数据:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication52
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            List<urun> uruns = doc.Descendants("urun").Select(x => new urun() {
                resimDosyasi = (string)x.Element("resimDosyasi"),
                aciklama = (string)x.Element("aciklama"),
                birim = (string)x.Element("birim"),
                miktar = (int)x.Element("miktar"),
                toplam = (string)x.Element("toplam")
            }).ToList();

        }
    }
    public class urun
    {
        public string resimDosyasi { get; set; }
        public string aciklama { get; set; }
        public string birim { get; set; }
        public int miktar { get; set; }
        public string toplam { get; set; }
    }
}
XmlSerializer serializer=新的XmlSerializer(typeof(Teklif));
使用(FileStream FileStream=newfilestream(filename,FileMode.Open))
{
Teklif结果=(Teklif)序列化程序。反序列化(fileStream);
}
//对象模型示例
[XmlRoot(“teklif”)]
公共级Teklif
{
[XmlElement()]
公共bilgiler bilgiler{get;set;}
}
公共级诈骗犯
{
[XmlElement()]公共字符串firma{get;set;}
[XmlElement()]公共字符串aciklama{get;set;}
[XmlElement()]公共字符串isim{get;set;}
[XmlElement()]公共字符串telefon{get;set;}
[XmlElement()]公共字符串eposta{get;set;}
[XmlArray()]
[XmlArrayItem(“urun”)]
公共列表urunler{get;set;}
}
公共类乌伦
{
[XmlElement()]公共字符串resimDosyasi{get;set;}
[XmlElement()]公共字符串aciklama{get;set;}
[XmlElement()]公共字符串birim{get;set;}
[XmlElement()]public int miktar{get;set;}
[XmlElement()]公共字符串toplam{get;set;}
}

表示选择节点,无论它们在当前上下文下的何处。当前上下文默认为根。要将结果限制为当前子节点下的结果,请遵循以下模式(只需添加一个点):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication52
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            List<urun> uruns = doc.Descendants("urun").Select(x => new urun() {
                resimDosyasi = (string)x.Element("resimDosyasi"),
                aciklama = (string)x.Element("aciklama"),
                birim = (string)x.Element("birim"),
                miktar = (int)x.Element("miktar"),
                toplam = (string)x.Element("toplam")
            }).ToList();

        }
    }
    public class urun
    {
        public string resimDosyasi { get; set; }
        public string aciklama { get; set; }
        public string birim { get; set; }
        public int miktar { get; set; }
        public string toplam { get; set; }
    }
}
XmlSerializer serializer = new XmlSerializer(typeof(Teklif));
using (FileStream fileStream = new FileStream(filename, FileMode.Open))
{
     Teklif result = (Teklif)serializer.Deserialize(fileStream);
}

// Object model example
[XmlRoot("teklif")]
public class Teklif
{
    [XmlElement()]
    public bilgiler bilgiler { get; set; }
}

public class bilgiler
{
    [XmlElement()] public string firma { get; set; }
    [XmlElement()] public string aciklama { get; set; }
    [XmlElement()] public string isim { get; set; }
    [XmlElement()] public string telefon { get; set; }
    [XmlElement()] public string eposta { get; set; }

    [XmlArray()]
    [XmlArrayItem("urun")]
    public List<Urun> urunler { get; set; }
}

public class Urun
{
    [XmlElement()] public string resimDosyasi { get; set; }
    [XmlElement()] public string aciklama { get; set; }
    [XmlElement()] public string birim { get; set; }
    [XmlElement()] public int miktar { get; set; }
    [XmlElement()] public string toplam { get; set; }
}
var urun_resim = childNode.SelectSingleNode(".//resimDosyasi").InnerText;