-2

如何将数据从xml文件拉到c#中的列表视图,获取xml文档标签内的数据..

我有一个这样的xml文件..

<?xml version="1.0"?>
<?xml-stylesheet href="file:///C:/Program Files (x86)/Nmap/nmap.xsl" type="text/xsl"?>
<!-- Nmap 6.25 scan initiated Fri Feb 15 13:34:46 2013 as: nmap -T4 -A -v -oX - 10.100.95.0 10.100.95.255 -->
<nmaprun scanner="nmap" args="nmap -T4 -A -v -oX - 10.100.95.0 10.100.95.255" start="1360915486" startstr="Fri Feb 15 13:34:46 2013" version="6.25" xmloutputversion="1.04">
<scaninfo type="syn" protocol="tcp" numservices="1000" services=""/>
<verbose level="1"/>
<debugging level="0"/>
<taskbegin task="ARP Ping Scan" time="1360915488"/>
<taskend task="ARP Ping Scan" time="1360915489" extrainfo="2 total hosts"/>
<host><status state="down" reason="no-response" reason_ttl="0"/>
<address addr="10.100.95.0" addrtype="ipv4"/>
</host>
<host><status state="down" reason="no-response" reason_ttl="0"/>
<address addr="10.100.95.255" addrtype="ipv4"/>
</host>
<runstats><finished time="1360915489" timestr="Fri Feb 15 13:34:49 2013" elapsed="3.16" summary="Nmap done at Fri Feb 15 13:34:49 2013; 2 IP addresses (0 hosts up) scanned in 3.16 seconds" exit="success"/><hosts up="0" down="2" total="2"/>
</runstats>
</nmaprun>

我想提取列表视图网格中地址标签内的所有addr地址和addrtype列表,并在列表视图的每一行中相应地显示它..

列表应该是这样的.. Addr Addrtype 192.168.1.45 ipv4 192.164.4.56 ipv4

ETC..

请帮助我提前谢谢..

4

2 回答 2

1

答案 1.- 使用 linq to XML 并在列表或集合中收集数据,之后您可以将数据与列表视图控件绑定。

答案 2.- 使用数据集

    Dim xmlDatapath As String = "yourxml file path\111.xml"

    Dim xmlData As String = File.ReadAllText(xmlDatapath)
    Dim stream As StringReader = New StringReader(xmlData)
    Dim reader As XmlTextReader = New XmlTextReader(stream)
    Dim xmlDS As New DataSet()
    xmlDS.ReadXml(reader)

并将您的数据与列表视图控件绑定。我希望这能帮到您。

于 2013-02-15T08:44:19.150 回答
0
var xdoc = XDocument.Load(@"c:\temp\arg\s.xml");
var addrs = xdoc.Descendants().Elements("address")
               .Select(y => new {
                                 Name = y.Attribute("addr").Value, 
                                 Value =   y.Attribute("addrtype").Value
                                });

这将为您提供所需的所有地址

foreach(var addr in addrs)
{
    ListViewItem item1 = new ListViewItem(addr.Name);
    item1.SubItems.Add(addr.Value);
    //We have a ListView named listview
    listview.Items.Add(item1);
}

或者我们可以像这样更简单

var addrs = xdoc.Descendants().Elements("address")
                   .Select(y => {
                                    ListViewItem item1 = new ListViewItem(y.Attribute("addr").Value);
                                    item1.SubItems.Add(y.Attribute("addrtype").Value);
                                    return item1;
                                });

listView.Items.AddRange(addrs);
于 2013-02-15T08:53:56.553 回答