Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#Xml选择节点包含节点包含内部文本包含文本_C# - Fatal编程技术网

C#Xml选择节点包含节点包含内部文本包含文本

C#Xml选择节点包含节点包含内部文本包含文本,c#,C#,我需要一些帮助 我有XML: <root xmlns="urn:schemas-upnp-org:device-1-0"> <specVersion> <major>1</major> <minor>0</minor> </specVersion> <URLBase>someip</URLBase> <devic

我需要一些帮助

我有XML:

<root
    xmlns="urn:schemas-upnp-org:device-1-0">
    <specVersion>
        <major>1</major>
        <minor>0</minor>
    </specVersion>
    <URLBase>someip</URLBase>
    <device>
        <deviceType>
urn:schemas-upnp-org:device:InternetGatewayDevice:1
</deviceType>
        <friendlyName>my_router</friendlyName>
        <manufacturer>Beeline</manufacturer>
        <manufacturerURL>my_providers_url</manufacturerURL>
        <modelDescription>Home Internet Center</modelDescription>
        <modelName>my_router</modelName>
        <modelNumber>Smart Box v1</modelNumber>
        <modelURL>my_providers_url</modelURL>
        <serialNumber>SF14F8F56358</serialNumber>
        <UDN>uuid:fc4ec57e-28b1-11db-88f8-a16830956233</UDN>
        <UPC>my_router</UPC>
        <serviceList>
            <service>
                <serviceType>urn:schemas-upnp-org:service:Layer3Forwarding:1</serviceType>
                <serviceId>urn:upnp-org:serviceId:L3Forwarding1</serviceId>
                <controlURL>/upnp/control/L3Forwarding1</controlURL>
                <eventSubURL>/upnp/event/L3Forwarding1</eventSubURL>
                <SCPDURL>/l3frwd.xml</SCPDURL>
            </service>
        </serviceList>
        <deviceList>
            <device>
                <deviceType>urn:schemas-upnp-org:device:WANDevice:1</deviceType>
                <friendlyName>WANDevice</friendlyName>
                <manufacturer>Beeline</manufacturer>
                <manufacturerURL>my_providers_url</manufacturerURL>
                <modelDescription>Home Internet Center</modelDescription>
                <modelName>my_router</modelName>
                <modelNumber>1</modelNumber>
                <modelURL>my_providers_url</modelURL>
                <serialNumber>SF14F8F56358</serialNumber>
                <UDN>uuid:fc4ec57e-2753-11db-88f8-0060085db3f6</UDN>
                <UPC>my_router</UPC>
                <serviceList>
                    <service>
                        <serviceType>
urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1
</serviceType>
                        <serviceId>urn:upnp-org:serviceId:WANCommonIFC1</serviceId>
                        <controlURL>/upnp/control/WANCommonIFC1</controlURL>
                        <eventSubURL>/upnp/event/WANCommonIFC1</eventSubURL>
                        <SCPDURL>/cmnicfg.xml</SCPDURL>
                    </service>
                </serviceList>
                <deviceList>
                    <device>
                        <deviceType>urn:schemas-upnp-org:device:WANConnectionDevice:1</deviceType>
                        <friendlyName>WANConnectionDevice</friendlyName>
                        <manufacturer>Beeline</manufacturer>
                        <manufacturerURL>my_providers_url</manufacturerURL>
                        <modelDescription>Home Internet Center</modelDescription>
                        <modelName>my_router</modelName>
                        <modelNumber>1</modelNumber>
                        <modelURL>my_providers_url</modelURL>
                        <serialNumber>SF14F8F56358</serialNumber>
                        <UDN>uuid:fc4ec57e-092a-11db-88f8-0578ab52457c</UDN>
                        <UPC>my_router</UPC>
                        <serviceList>
                            <service>
                                <serviceType>
urn:schemas-upnp-org:service:WANEthernetLinkConfig:1
</serviceType>
                                <serviceId>urn:upnp-org:serviceId:WANEthLinkC1</serviceId>
                                <controlURL>/upnp/control/WANEthLinkC1</controlURL>
                                <eventSubURL>/upnp/event/WANEthLinkC1</eventSubURL>
                                <SCPDURL>/wanelcfg.xml</SCPDURL>
                            </service>
                            <service>
                                <serviceType>urn:schemas-upnp-org:service:WANPPPConnection:1</serviceType>
                                <serviceId>urn:upnp-org:serviceId:WANPPPConn1</serviceId>
                                <controlURL>/upnp/control/WANPPPConn1</controlURL>
                                <eventSubURL>/upnp/event/WANPPPConn1</eventSubURL>
                                <SCPDURL>/pppcfg.xml</SCPDURL>
                            </service>
                        </serviceList>
                    </device>
                </deviceList>
            </device>
            <device>
                <deviceType>urn:schemas-upnp-org:device:LANDevice:1</deviceType>
                <friendlyName>LANDevice</friendlyName>
                <manufacturer>Beeline</manufacturer>
                <manufacturerURL>my_providers_url</manufacturerURL>
                <modelDescription>Home Internet Center</modelDescription>
                <modelName>my_router</modelName>
                <modelNumber>1</modelNumber>
                <modelURL>my_providers_url</modelURL>
                <serialNumber>SF14F8F56358</serialNumber>
                <UDN>uuid:fc4ec57e-ba12-11db-88f8-a72469cbac1a</UDN>
                <UPC>my_router</UPC>
                <serviceList>
                    <service>
                        <serviceType>
urn:schemas-upnp-org:service:LANHostConfigManagement:1
</serviceType>
                        <serviceId>urn:upnp-org:serviceId:LANHostCfg1</serviceId>
                        <controlURL>/upnp/control/LANHostCfg1</controlURL>
                        <eventSubURL>/upnp/event/LANHostCfg1</eventSubURL>
                        <SCPDURL>/lanhostc.xml</SCPDURL>
                    </service>
                </serviceList>
            </device>
        </deviceList>
        <presentationURL>someip</presentationURL>
    </device>
</root>

1.
0
萨米普
urn:schemas upnp组织:设备:InternetGatewayDevice:1
我的路由器
直线
我的\u提供者\u url
家庭互联网中心
我的路由器
智能箱v1
我的\u提供者\u url
SF14F8F56358
uuid:fc4ec57e-28b1-11db-88f8-a16830956233
我的路由器
urn:schemas upnp组织:服务:层3转发:1
urn:upnp组织:serviceId:L3Forwarding1
/upnp/control/L3Forwarding1
/upnp/event/L3Forwarding1
/l3frwd.xml
urn:schemas upnp组织:设备:WANDevice:1
WANDevice
直线
我的\u提供者\u url
家庭互联网中心
我的路由器
1.
我的\u提供者\u url
SF14F8F56358
uuid:fc4ec57e-2753-11db-88f8-0060085db3f6
我的路由器
urn:schemas upnp组织:服务:WANComonInterfaceConfig:1
urn:upnp组织:serviceId:WANCommonIFC1
/upnp/控制/WANComonifc1
/upnp/event/WANComonifc1
/cmnicfg.xml
urn:schemas upnp组织:设备:WANConnectionDevice:1
WAN连接设备
直线
我的\u提供者\u url
家庭互联网中心
我的路由器
1.
我的\u提供者\u url
SF14F8F56358
uuid:fc4ec57e-092a-11db-88f8-0578ab52457c
我的路由器
urn:schemas upnp org:service:WANEthernetLinkConfig:1
urn:upnp组织:serviceId:WANEthLinkC1
/upnp/控制/WANEthLinkC1
/upnp/event/WANEthLinkC1
/wanelcfg.xml
urn:schemas upnp组织:服务:WANPPP连接:1
urn:upnp组织:serviceId:WANPPPConn1
/upnp/控制/WANPPPPCONN1
/upnp/event/WANPPPConn1
/pppcfg.xml
urn:schemas upnp组织:设备:LANDevice:1
陆地设备
直线
我的\u提供者\u url
家庭互联网中心
我的路由器
1.
我的\u提供者\u url
SF14F8F56358
uuid:fc4ec57e-ba12-11db-88f8-a72469cbac1a
我的路由器
urn:schemas upnp组织:服务:LANHostConfigManagement:1
urn:upnp组织:serviceId:LANHostCfg1
/upnp/control/LANHostCfg1
/upnp/event/LANHostCfg1
/lanhostc.xml
萨米普
我需要找到此节点:

<service>
                                <serviceType>urn:schemas-upnp-org:service:WANPPPConnection:1</serviceType>
                                <serviceId>urn:upnp-org:serviceId:WANPPPConn1</serviceId>
                                <controlURL>/upnp/control/WANPPPConn1</controlURL>
                                <eventSubURL>/upnp/event/WANPPPConn1</eventSubURL>
                                <SCPDURL>/pppcfg.xml</SCPDURL>
                            </service>
<controlURL>/upnp/control/WANPPPConn1</controlURL>

urn:schemas upnp组织:服务:WANPPP连接:1
urn:upnp组织:serviceId:WANPPPConn1
/upnp/控制/WANPPPPCONN1
/upnp/event/WANPPPConn1
/pppcfg.xml
通过此标记和其中的innertext:

<serviceType>urn:schemas-upnp-org:service:WANPPPConnection:1</serviceType>
urn:schemas-upnp-org:service:wanpppcconnection:1
但这也可能是这样的:

<serviceType>urn:schemas-upnp-org:service:WANIPConnection:1</serviceType>
urn:schemas-upnp-org:service:WANIPConnection:1
找到此节点后,我希望从此节点获取innertext:

<service>
                                <serviceType>urn:schemas-upnp-org:service:WANPPPConnection:1</serviceType>
                                <serviceId>urn:upnp-org:serviceId:WANPPPConn1</serviceId>
                                <controlURL>/upnp/control/WANPPPConn1</controlURL>
                                <eventSubURL>/upnp/event/WANPPPConn1</eventSubURL>
                                <SCPDURL>/pppcfg.xml</SCPDURL>
                            </service>
<controlURL>/upnp/control/WANPPPConn1</controlURL>
/upnp/control/WANPPPConn1
我怎样才能做到这一切?

正是您所需要的


然后试试这样的。。。要使用,请将XML保存到文件并复制代码。它将把整个XML反序列化为一个对象,这样您就可以更轻松地管理它了

使用

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
班级

[XmlRoot(ElementName = "specVersion", Namespace = "urn:schemas-upnp-org:device-1-0")]
public class SpecVersion
{
    [XmlElement(ElementName = "major", Namespace = "urn:schemas-upnp-org:device-1-0")]
    public string Major { get; set; }
    [XmlElement(ElementName = "minor", Namespace = "urn:schemas-upnp-org:device-1-0")]
    public string Minor { get; set; }
}

[XmlRoot(ElementName = "service", Namespace = "urn:schemas-upnp-org:device-1-0")]
public class Service
{
    [XmlElement(ElementName = "serviceType", Namespace = "urn:schemas-upnp-org:device-1-0")]
    public string ServiceType { get; set; }
    [XmlElement(ElementName = "serviceId", Namespace = "urn:schemas-upnp-org:device-1-0")]
    public string ServiceId { get; set; }
    [XmlElement(ElementName = "controlURL", Namespace = "urn:schemas-upnp-org:device-1-0")]
    public string ControlURL { get; set; }
    [XmlElement(ElementName = "eventSubURL", Namespace = "urn:schemas-upnp-org:device-1-0")]
    public string EventSubURL { get; set; }
    [XmlElement(ElementName = "SCPDURL", Namespace = "urn:schemas-upnp-org:device-1-0")]
    public string SCPDURL { get; set; }
}

[XmlRoot(ElementName = "serviceList", Namespace = "urn:schemas-upnp-org:device-1-0")]
public class ServiceList
{
    [XmlElement(ElementName = "service", Namespace = "urn:schemas-upnp-org:device-1-0")]
    public List<Service> Service { get; set; }
}

[XmlRoot(ElementName = "device", Namespace = "urn:schemas-upnp-org:device-1-0")]
public class Device
{
    [XmlElement(ElementName = "deviceType", Namespace = "urn:schemas-upnp-org:device-1-0")]
    public string DeviceType { get; set; }
    [XmlElement(ElementName = "friendlyName", Namespace = "urn:schemas-upnp-org:device-1-0")]
    public string FriendlyName { get; set; }
    [XmlElement(ElementName = "manufacturer", Namespace = "urn:schemas-upnp-org:device-1-0")]
    public string Manufacturer { get; set; }
    [XmlElement(ElementName = "manufacturerURL", Namespace = "urn:schemas-upnp-org:device-1-0")]
    public string ManufacturerURL { get; set; }
    [XmlElement(ElementName = "modelDescription", Namespace = "urn:schemas-upnp-org:device-1-0")]
    public string ModelDescription { get; set; }
    [XmlElement(ElementName = "modelName", Namespace = "urn:schemas-upnp-org:device-1-0")]
    public string ModelName { get; set; }
    [XmlElement(ElementName = "modelNumber", Namespace = "urn:schemas-upnp-org:device-1-0")]
    public string ModelNumber { get; set; }
    [XmlElement(ElementName = "modelURL", Namespace = "urn:schemas-upnp-org:device-1-0")]
    public string ModelURL { get; set; }
    [XmlElement(ElementName = "serialNumber", Namespace = "urn:schemas-upnp-org:device-1-0")]
    public string SerialNumber { get; set; }
    [XmlElement(ElementName = "UDN", Namespace = "urn:schemas-upnp-org:device-1-0")]
    public string UDN { get; set; }
    [XmlElement(ElementName = "UPC", Namespace = "urn:schemas-upnp-org:device-1-0")]
    public string UPC { get; set; }
    [XmlElement(ElementName = "serviceList", Namespace = "urn:schemas-upnp-org:device-1-0")]
    public ServiceList ServiceList { get; set; }

    [XmlElement(ElementName = "deviceList", Namespace = "urn:schemas-upnp-org:device-1-0")]
    public DeviceList DeviceList { get; set; }
}

[XmlRoot(ElementName = "deviceList", Namespace = "urn:schemas-upnp-org:device-1-0")]
public class DeviceList
{
    [XmlElement(ElementName = "device", Namespace = "urn:schemas-upnp-org:device-1-0")]
    public Device Device { get; set; }
}

[XmlRoot(ElementName = "root", Namespace = "urn:schemas-upnp-org:device-1-0")]
public class Root
{
    [XmlElement(ElementName = "specVersion", Namespace = "urn:schemas-upnp-org:device-1-0")]
    public SpecVersion SpecVersion { get; set; }
    [XmlElement(ElementName = "URLBase", Namespace = "urn:schemas-upnp-org:device-1-0")]
    public string URLBase { get; set; }
    [XmlElement(ElementName = "device", Namespace = "urn:schemas-upnp-org:device-1-0")]
    public Device Device { get; set; }
    [XmlAttribute(AttributeName = "xmlns")]
    public string Xmlns { get; set; }
}

OK不是最短的答案,但希望它能有所帮助。…

好的,谢谢,请告诉我,所有的课程都是你自己写的,还是有一个这样做的程序?对不起,英语不好。你可以试试这个。。。有时(与XML一样)它不能100%地理解它,我不得不修改其中一个类以使其工作(可能类中缺少更多属性)。我认为这是因为您的XML有几个同名的节点。记住为选中的答案选择\up投票…感谢您的回答,但我尝试了xpath,但它对我不起作用