【问题标题】:How to iterate the data from XMLElement using foreach in C#如何在 C# 中使用 foreach 从 XMLElement 中迭代数据
【发布时间】:2019-10-12 10:33:52
【问题描述】:

我想从 XMLElement 中迭代数据。在列表中我有 10 行记录。每行再次包含值列表。在行中,每个值都需要存储为 somString 类型。

下面是从 List 获取的 XMLElement

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

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

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 行记录。每行再次包含值列表。在行中,每个值都需要存储到 somString 类型中。

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

请为此参考一些示例。

【问题讨论】:

标签: c# ssis mdm


【解决方案1】:

这是获取值的快速和脏方法,可以通过删除硬编码的值并在访问它们之前检查对象来改善这一点。

编辑:XML 结构来自here。

            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("----------------------------------");
                    }
                }
            }

【讨论】:

  • 感谢您的回复,我使用了其他方法来过滤记录
【解决方案2】:

使用(WebResponse 响应 = httpWebRequest.GetResponse()) { 使用 (StreamReader streamreader = new StreamReader(response.GetResponseStream())) { 字符串结果1 = streamreader.ReadToEnd(); MessageBox.Show(result1);

                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;






                    }
                }               

            }
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-06
    • 2017-05-24
    相关资源
    最近更新 更多