gpt4 book ai didi

c# - 具有不同属性的对象的多个值的 XML 到 Linq

转载 作者:太空宇宙 更新时间:2023-11-03 14:55:51 26 4
gpt4 key购买 nike

大家好,当我在使用 powershell 工作的公司扫描网络时,我将结果转换为 Xml,但我发现从 XML 文件中获取数据很困难。这是 XML 文件的示例:

<?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 但没有得到主机名和其他属性 vlues。这是设备类

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!");

最佳答案

使用 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();

}

}
}

关于c# - 具有不同属性的对象的多个值的 XML 到 Linq,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49208252/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com