首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在C#中使用foreach迭代来自XMLElement的数据

如何在C#中使用foreach迭代来自XMLElement的数据
EN

Stack Overflow用户
提问于 2019-10-12 18:33:52
回答 2查看 396关注 0票数 0

我想迭代XMLElement中的数据。在列表中我有10行记录。每一行都包含同样的值列表。在row中,每个值都需要存储到somString类型中。

下面是从列表中获取的XMLElement

代码语言:javascript
复制
<ROW id="1">
   <D n="6721">10128</D>
   <D n="6724">CL</D>
   <D n="6771">*</D>
   <D n="6773">ACT</D>
   <D n="6774">PHON</D>
   <D n="6775">04-MAR-2018 21:54</D>
   <D n="6779">MEP-IU</D>
   <D n="6780">MEP-IU-010</D>
   <D n="6782">CWP2B19-113</D>
   <D n="6792">11410</D>
   <D n="6809"/>
   <D n="6880"/>
   <D n="11651">Tap is not working in the Back of the Apt  
                Name: Alex 
                Contact : 971-566826978</D>
   <D n="100410">40977</D>
   <D n="101312">AHMED.ALI@MERAAS.AE</D>
   <D n="101313">HANDOVER</D>
 </ROW>

我尝试了这段代码,因为我试图迭代数据,但我只得到了第一个元素,而不是剩余的元素:

代码语言:javascript
复制
for (int i = 0; i < nodeList.Count; i++)
{
    if (nodeList[i].InnerText.Length > 0)
    {
        string name =(string) i.FirstChild.Value;                     
        MessageBox.Show(nodeList[i].InnerText);
    }
}

但是我只获取已归档的列值。如何从XMLElement中获取剩余数据。

在列表中我有10行记录。每一行都包含同样的值列表。在row中,每个值都需要存储到somString类型中。

我想从响应中提取所有记录的列表。我不知道如何编写代码来过滤数据。

请参考一些样品来做这个。

EN

回答 2

Stack Overflow用户

发布于 2019-10-14 01:04:58

这是一种获取这些值的quick 脏方法,可以通过删除硬编码的值并在访问它们之前检查对象来改进。

编辑: XML结构来自here

代码语言:javascript
复制
            using (var stream = new StreamReader("Response.xml"))
            {
                XDocument doc = XDocument.Load(stream);

                var nodes = doc.Elements().Descendants()
                    .Where(x => x.Name == XName.Get("ROW", "http://schemas.datastream.net/MP_functions/MP0118_GetGridHeaderData_001_Result"));

                foreach (var node in nodes)
                {
                    if (node.FirstAttribute != null)
                    {
                        var firstAttribute = node.FirstAttribute;
                        Console.WriteLine($"{firstAttribute.Name.LocalName} - {firstAttribute.Value}");

                        var children = node.Descendants();
                        if (children.Count() > 0)
                        {
                            foreach (var child in children)
                            {
                                Console.WriteLine($"{child.FirstAttribute.Value}:{child.Value}");
                            }
                        }
                        Console.WriteLine("----------------------------------");
                    }
                }
            }
票数 0
EN

Stack Overflow用户

发布于 2019-10-14 14:15:29

using (WebResponse response = httpWebRequest.GetResponse()) { using (StreamReader streamreader =新StreamReader(response.GetResponseStream(){ string result1 = streamreader.ReadToEnd();MessageBox.Show(result1);

代码语言:javascript
复制
                xmlDocument = new XmlDocument();
                xmlDocument.LoadXml(result1);
                XmlNodeList nodeList;

                if (xmlDocument.DocumentElement.Attributes["xmlns:soapenv"] != null)
                {
                    string xmlns = xmlDocument.DocumentElement.Attributes["xmlns:soapenv"].Value;
                    XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDocument.NameTable);

                    nsmgr.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
                    nsmgr.AddNamespace("df", "http://schemas.datastream.net/MP_functions/MP0118_GetGridHeaderData_001_Result");


                    nodeList = xmlDocument.SelectNodes("/soapenv:Envelope/soapenv:Body/df:MP0118_GetGridHeaderData_001_Result/df:GRIDRESULT/df:GRID/df:DATA/df:ROW[*]", nsmgr);
                }
                else
                {
                    nodeList = xmlDocument.SelectNodes("/soapenv:Envelope/soapenv:Body/df:MP0118_GetGridHeaderData_001_Result/df:GRIDRESULT/df:GRID/df:DATA/df:ROW[*]");
                }


                for (int i = 0; i < nodeList.Count; i++)
                {
                    if (nodeList[i].InnerText.Length > 0)
                    {




                        String ctr_code = nodeList[i].ChildNodes[0].InnerText;
                        String ctr_status = nodeList[i].ChildNodes[1].InnerText;
                        String ctr_org = nodeList[i].ChildNodes[2].InnerText;
                        String ctr_type = nodeList[i].ChildNodes[3].InnerText;
                        String ctr_contactsource = nodeList[i].ChildNodes[4].InnerText;
                        String ctr_created = nodeList[i].ChildNodes[5].InnerText;
                        String ctr_servicecategory = nodeList[i].ChildNodes[6].InnerText;
                        String ctr_serviceproblem = nodeList[i].ChildNodes[7].InnerText;
                        String ctr_object = nodeList[i].ChildNodes[8].InnerText;
                        String ctr_contactinfoid = nodeList[i].ChildNodes[9].InnerText;
                        String ctr_contactnote = nodeList[i].ChildNodes[10].InnerText;
                        String ctr_desc = nodeList[i].ChildNodes[11].InnerText;
                        String ctr_note = nodeList[i].ChildNodes[12].InnerText;
                        String ctr_event = nodeList[i].ChildNodes[13].InnerText;
                        String ctr_createdby = nodeList[i].ChildNodes[14].InnerText;
                        String ctr_mrc = nodeList[i].ChildNodes[15].InnerText;






                    }
                }               

            }
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58353224

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档