XML到Linq,具有不同属性

时间:2018-03-10 10:56:33

标签: c# xml linq

HI大家当我在使用powershell工作的公司中扫描网络时,我将结果转换为Xml比我发现diffucilty从XML文件中获取数据。 这里是XML FIle的例子:

<?xml version="1.0" encoding="utf-8"?>
<Objects>
  <Object Type="System.Management.Automation.PSCustomObject">
    <Property Name="IPv4Address" Type="System.Net.IPAddress">10.4.0.6</Property>
    <Property Name="Status" Type="System.String">Up</Property>
    <Property Name="Hostname" Type="System.String">1200-855-01P</Property>
    <Property Name="MAC" Type="System.String">00-0A-F7-16-07-83</Property>
    <Property Name="Vendor" Type="System.String">Broadcom</Property>
  </Object>
  <Object Type="System.Management.Automation.PSCustomObject">
    <Property Name="IPv4Address" Type="System.Net.IPAddress">10.4.0.10</Property>
    <Property Name="Status" Type="System.String">Up</Property>
    <Property Name="Hostname" Type="System.String">1200-850-01O</Property>
    <Property Name="MAC" Type="System.String">00-0A-F7-16-08-BD</Property>
    <Property Name="Vendor" Type="System.String">Broadcom</Property>
  </Object>
</Objects>

请帮我从这个XML文件中获取数据。 感谢

我试过没有结果。我使用设备类创建类以从属性元素获取IPadress。我得到了IPadress,但没有找到主机名和其他属性的线索。这是设备类

namespace ConsoleApplication1
    {
    public class Device
        {
        public string IpAdress { get; set; }
        public string Statut { get; set; }
        public string MacAdress { get; set; }
        public string Hostname { get; set; }
        public string Vendor { get; set; }

        public static Collection<Device> SetDeviceData = new Collection<Device>();
        public static Collection<Device> getDeviceData { get { return SetDeviceData; } }

        }
    }

这是我的代码:

Device dv = new Device();
            dv.IpAdress = string.Empty; dv.Statut = string.Empty; dv.MacAdress = string.Empty;
            dv.Hostname = string.Empty;
            dv.Vendor = string.Empty;
            Console.WriteLine("Reading");
                        XElement xelement = XElement.Load("..\\Scripts\\primaire.xml");

            var homePhone = from phoneno in xelement.Elements("Object")
                            where (string)phoneno.Element("Property").Attribute("Name") == "IPv4Address"
                            select phoneno;
            Console.WriteLine("List HomePhone Nos.");
            foreach(XElement xEle in homePhone)
                {
                Console.WriteLine(xEle.Element("Property").Value);
                }

            Console.WriteLine("Done!");

1 个答案:

答案 0 :(得分:0)

使用Xml Linq非常简单。请参阅以下代码:

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

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            new Device(FILENAME);
        }
    }
    public class Device
    {
        public string IpAdress { get; set; }
        public string Statut { get; set; }
        public string MacAdress { get; set; }
        public string Hostname { get; set; }
        public string Vendor { get; set; }

        public static List<Device> getDeviceData { get; set; }

        public Device() { }
        public Device(string filename)
        {
            XDocument doc = XDocument.Load(filename);

            Device.getDeviceData = doc.Descendants("Object").Select(x => new Device()
            {
                IpAdress = x.Elements("Property").Where(y => (string)y.Attribute("Name") == "IPv4Address").Select(z => (string)z).FirstOrDefault(),
                Statut = x.Elements("Property").Where(y => (string)y.Attribute("Name") == "Status").Select(z => (string)z).FirstOrDefault(),
                MacAdress = x.Elements("Property").Where(y => (string)y.Attribute("Name") == "MAC").Select(z => (string)z).FirstOrDefault(),
                Hostname = x.Elements("Property").Where(y => (string)y.Attribute("Name") == "Hostname").Select(z => (string)z).FirstOrDefault(),
                Vendor = x.Elements("Property").Where(y => (string)y.Attribute("Name") == "Vendor").Select(z => (string)z).FirstOrDefault()
            }).ToList();

        }

    }
}