Reputation: 113
I need some help.
I have 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>
I need to find this node:
<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>
By this tag and innertext in it:
<serviceType>urn:schemas-upnp-org:service:WANPPPConnection:1</serviceType>
But this can also look like that:
<serviceType>urn:schemas-upnp-org:service:WANIPConnection:1</serviceType>
After finding this node I want to get innertext from this node:
<controlURL>/upnp/control/WANPPPConn1</controlURL>
How can I do this all?
Upvotes: 1
Views: 426
Reputation: 1556
Try something like this... To use, save your XML to a file and copy the code. It will Deserialize the whole XML to an object then you can manage it easier....
Usings...
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
Classes....
[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; }
}
Code...
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);
}
OK is not the shortest answer, but hope it helps....
Upvotes: 1
Reputation: 3153
Select Nodes Using XPath Navigation is what you needed.
and XPath Example
Upvotes: 0