将xml存储到arraylist [英] store xml into arraylist

查看:68
本文介绍了将xml存储到arraylist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想将xml节点值之一存储到数组列表中.我的xml文件如下所示:

hi guys,

I want to store one of the xml node value into array list. My xml file look like this:

<creategroup>
  <group>
    <id>sdas</id>
    <name>as</name>
  </group>
  <group>
    <id>ss</id>
    <name>sd</name>
  </group>
</creategroup>


我只想将名称存储到数组列表中.

请帮助我.
谢谢!


I want to store only a name into array list.

Please help me with this.
Thanks!

推荐答案



我尝试了一些符合您要求的代码,只需检查一次

Hi,

I tried some code for your requirement just check this once

 XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load(Server.MapPath("~/SomeFiles/XMLFile2.xml"));
        XmlNodeList xmlnodli = xmldoc.SelectNodes("creategroup/group/name");
        List<string> strli = new List<string>();
        foreach (XmlNode xm in xmlnodli)
        {
            strli.Add(xm.InnerText);
        }
</string></string>



在上面的代码中,您必须将xmlpath传递给加载方法

并且selectNodes()用于基于xpath检索特定节点.
xpath就是节点流

我希望它也对您有用.

所有Best



In the above code you''ve to pass xmlpath to load method

And selectNodes() is used for retrieve particular node based on xpath.
xpath is nothing but flow of nodes

I hope it works for you also.

All the Best


.Net都具有广泛的类集,可帮助您立即完成此操作.
在您的项目中包含System.Xml命名空间.您可以在此处阅读- MSDN- System.Xml [ ^ ]

这是您需要的示例:
按名称选择XML节点[C#] [
.Net has extensive set of classes that will help you do this in no time.
Include System.Xml namespace into your project. You can read about it here - MSDN- System.Xml[^]

Here is an example for your requirement:
Select XML Nodes by Name [C#][^]

Hope this helps!


首先,请不要使用ArrayList.早在.NET Framework 2.0中,通过引入泛型,该类对于新开发已被淘汰.使用System.Collections.Generic.List http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx [^ ].与非泛型类不同,泛型类不需要不太安全的类型转换.

我真的不知道为什么要对XML使用列表类型. XML具有树状结构.您只能使用列表类,因为您可以轻松地使用列表表示树(我必须解释如何?).但是也许您真正需要的是DOM结构.在这种情况下,请使用类System.Xml.XmlDocument,请参见 http://msdn.microsoft. com/en-us/library/system.xml.xmldocument.aspx [ ^ ].如果需要,可以将DOM结构映射到任何其他结构.

—SA
To start with, don''t use ArrayList. This class is rendered obsolete for new development by introduction of generics as early as in the .NET Framework 2.0. Use System.Collections.Generic.List, http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx[^]. Generic classes, unlike non-generic ones, do not require less safe type casting.

I really don''t know why using list types for XML. XML has a tree-like structure. You can use list classes only because you can easily represent a tree using a list (do I have to explain how?). But maybe what you really need is DOM structure. In this case, use the class System.Xml.XmlDocument, see http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^]. If you want, you can map DOM structure onto any other structure.

—SA


这篇关于将xml存储到arraylist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆