C#Xml选择节点包含的节点包含innertext包含的文本

时间:2016-03-19 15:00:34

标签: c#

我需要一些帮助。

我有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>

我需要找到这个节点:

<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>

通过此标记和innertext:

<serviceType>urn:schemas-upnp-org:service:WANPPPConnection:1</serviceType>

但这也可能是这样的:

<serviceType>urn:schemas-upnp-org:service:WANIPConnection:1</serviceType>

找到此节点后,我想从此节点获取innertext:

<controlURL>/upnp/control/WANPPPConn1</controlURL>

我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

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

... Usings

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; }
}

...代码

    static void Main(string[] args)
    {
        Root dezerializedXML = new Root();
        // Deserialize to object
        XmlSerializer serializer = new XmlSerializer(typeof(Root));
        using (FileStream stream = File.OpenRead(@"xml.xml"))
        {
            dezerializedXML = (Root)serializer.Deserialize(stream);
        } // Put a break-point here, then mouse-over dezerializedXML

        // the next line will get both of the services at that level in your XML you can probably do something with that
        var v = (dezerializedXML.Device.DeviceList.Device.DeviceList.Device.ServiceList.Service);
    }

确定不是最短的答案,但希望它有所帮助......

答案 1 :(得分:0)