Define and Deserialize empty classes - c#

I'm getting XML Data which contains an "envelope". For brevity, I'm focused on "Custom Fields" which is a collection of "Field".
Here is the part of the class definitions giving me a problem:
namespace Model.DocuSignEnvelope
{
[XmlRoot(ElementName = "CustomFields", Namespace = "http://www.docusign.net/API/3.0")]
public class CustomFields
{
[XmlElement(ElementName = "CustomField", Namespace = "http://www.docusign.net/API/3.0")]
public List<CustomField> CustomField { get; set; }
}
[XmlRoot(ElementName = "CustomField", Namespace = "http://www.docusign.net/API/3.0")]
public class CustomField
{
[XmlElement(ElementName = "Name", Namespace = "http://www.docusign.net/API/3.0")]
public string Name { get; set; } = "";
[XmlElement(ElementName = "Show", Namespace = "http://www.docusign.net/API/3.0")]
public string Show { get; set; } = "";
[XmlElement(ElementName = "Required", Namespace = "http://www.docusign.net/API/3.0")]
public string Required { get; set; } = "";
[XmlElement(ElementName = "Value", Namespace = "http://www.docusign.net/API/3.0")]
public string Value { get; set; } = "";
[XmlElement(ElementName = "CustomFieldType", Namespace = "http://www.docusign.net/API/3.0")]
public string CustomFieldType { get; set; } = "";
}
}
public static class DocuSignExtensionMethods
{
public static T DeserializeXmlX<T>(this string input) where T : class
{
XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(T));
using StringReader sr = new StringReader(input);
return (T)ser.Deserialize(sr);
}
}
if I get an empty list, happy times, it deserializes without issue:
<?xml version="1.0" encoding="utf-8"?>
<DocuSignEnvelopeInformation
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.docusign.net/API/3.0">
<EnvelopeStatus>
<RecipientStatuses>
<RecipientStatus>
<Type>Signer</Type>
...
<CustomFields />
...
but if I get a list of "empty" Custom Field
<?xml version="1.0" encoding="utf-8"?>
<DocuSignEnvelopeInformation
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.docusign.net/API/3.0">
<EnvelopeStatus>
<RecipientStatuses>
<RecipientStatus>
<Type>Signer</Type>
...
<CustomFields>
<CustomField />
<CustomField />
<CustomField />
</CustomFields>
...
It errors out with the Exception:
- ex {"There is an error in XML document (20, 7)."} System.Exception {System.InvalidOperationException}
- InnerException {"ReadElementContentAs() methods cannot be called on an element that has child elements. Line 20, position 7."} System.Exception {System.Xml.XmlException}
How can I tell the Deserializer to deserialize those fields as either an empty List (no valid entries) or a list with 3 empty CustomFields.

The following code works :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(EnvelopeInfo));
EnvelopeInfo fields = (EnvelopeInfo)serializer.Deserialize(reader);
}
}
[XmlRoot(ElementName = "EnvelopeInfo", Namespace = "http://www.docusign.net/API/3.0")]
public class EnvelopeInfo
{
[XmlElement(ElementName = "EnvelopeStatus", Namespace = "http://www.docusign.net/API/3.0")]
public EnvelopeStatus EnvelopeStatus { get; set; }
}
public class EnvelopeStatus
{
[XmlArray(ElementName = "RecipientStatuses", Namespace = "http://www.docusign.net/API/3.0")]
[XmlArrayItem(ElementName = "RecipientStatus", Namespace = "http://www.docusign.net/API/3.0")]
public RecipientStatus[] RecipientStatus { get; set; }
}
public class RecipientStatus
{
[XmlElement(ElementName = "CustomFields", Namespace = "http://www.docusign.net/API/3.0")]
public CustomFields CustomFields { get; set; }
}
public class CustomFields
{
[XmlElement(ElementName = "CustomField", Namespace = "http://www.docusign.net/API/3.0")]
public List<CustomField> CustomField { get; set; }
}
[XmlRoot(ElementName = "CustomField", Namespace = "http://www.docusign.net/API/3.0")]
public class CustomField
{
[XmlElement(ElementName = "Name", Namespace = "http://www.docusign.net/API/3.0")]
public string Name { get; set; }
[XmlElement(ElementName = "Show", Namespace = "http://www.docusign.net/API/3.0")]
public string Show { get; set; }
[XmlElement(ElementName = "Required", Namespace = "http://www.docusign.net/API/3.0")]
public string Required { get; set; }
[XmlElement(ElementName = "Value", Namespace = "http://www.docusign.net/API/3.0")]
public string Value { get; set; }
[XmlElement(ElementName = "CustomFieldType", Namespace = "http://www.docusign.net/API/3.0")]
public string CustomFieldType { get; set; }
}
}
Used following XML
<?xml version="1.0" encoding="utf-8"?>
<EnvelopeInfo xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.docusign.net/API/3.0">
<EnvelopeStatus>
<RecipientStatuses>
<RecipientStatus>
<Type>Signer</Type>
<CustomFields>
<CustomField />
<CustomField />
<CustomField />
</CustomFields>
</RecipientStatus>
</RecipientStatuses>
</EnvelopeStatus>
</EnvelopeInfo>

Related

Deserialize XML Not Working contains xmlns

This is my xml string.
<test name="james" type="seoul" xmlns="test:client">
<friends xmlns="friends:test">
<friend xmlns="" name="john">
Google
</friend>
<friend xmlns="" name="john">
Amazon
</friend>
</friends>
</test>
And i tried this code behind.
[Serializable()]
[XmlRoot(ElementName = "test", Namespace = "test:client")]
public class TestModel
{
public TestModel()
{
}
[XmlAttribute("name")]
public string Name { get; set; }
[XmlAttribute("type")]
public string Id { get; set; }
}
I can never change the xml. and I want to make a list of contents in friends element.
please help me!!
Try following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(TestModel));
TestModel testModel = (TestModel)serializer.Deserialize(reader);
}
}
[XmlRoot(ElementName = "test", Namespace = "test:client")]
public class TestModel
{
[XmlAttribute("name")]
public string Name { get; set; }
[XmlAttribute("type")]
public string Id { get; set; }
[XmlArray(ElementName = "friends", Namespace = "friends:test")]
[XmlArrayItem("friend", Namespace = "")]
public List<Friend> friends { get; set; }
}
public class Friend
{
[XmlText]
public string text { get; set; }
}
}

Deserialize soap response with multiple namespaces

I am having a very hard time trying to deserialize the soap response below.
I assume its because of the multiple namespaces or maybe because of the complex type (Serialization Array)
Soapformatter throws an Object reference exception and other more manual deserializations are returning an empty object. At this point I can only assume that I am not tagging my object correctly. What is the correct way to build out the BatchResponse object below so that it can deserialize from this response?
<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header>
<a:Action s:mustUnderstand="1">https://example.com/operations/fetch/BatchResponse</a:Action>
</s:Header>
<s:Body>
<BatchResponse xmlns="https://example.com/operations">
<BatchResult xmlns:b="https://example.com/responses" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<b:FailureMessages xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/Arrays" i:nil="true" />
<b:FailureType i:nil="true" />
<b:ProcessedSuccessfully>true</b:ProcessedSuccessfully>
<b:SessionId>1961810</b:SessionId>
<b:TotalPages>38</b:TotalPages>
</BatchResult>
</BatchResponse>
</s:Body>
</s:Envelope>
Also assuming Soapformatter continues to throw exceptions - what is the correct way to "xpath" to the BatchResponse object so that I can extract and deserialize?
Thanks
Try following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication131
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
Envelope envelope = (Envelope)serializer.Deserialize(reader);
}
}
[XmlRoot(ElementName = "Envelope", Namespace = "http://www.w3.org/2003/05/soap-envelope")]
public class Envelope
{
[XmlElement(ElementName = "Header", Namespace = "http://www.w3.org/2003/05/soap-envelope")]
public Header header { get; set; }
[XmlElement(ElementName = "Body", Namespace = "http://www.w3.org/2003/05/soap-envelope")]
public Body body { get; set; }
}
[XmlRoot(ElementName = "Header", Namespace = "http://www.w3.org/2003/05/soap-envelope")]
public class Header
{
[XmlElement(ElementName = "Action", Namespace = "http://www.w3.org/2005/08/addressing")]
public Action action { get; set; }
}
[XmlRoot(ElementName = "Action", Namespace = "http://www.w3.org/2005/08/addressing")]
public class Action
{
[XmlAttribute(AttributeName = "mustUnderstand", Namespace = "http://www.w3.org/2003/05/soap-envelope")]
public int mustUnderstand { get; set;}
[XmlText]
public string value { get;set;}
}
[XmlRoot(ElementName = "Body", Namespace = "")]
public class Body
{
[XmlElement(ElementName = "BatchResponse", Namespace = "https://example.com/operations")]
public BatchResponse batchResponse { get; set; }
}
[XmlRoot(ElementName = "BatchResponse", Namespace = "https://example.com/operations")]
public class BatchResponse
{
[XmlElement(ElementName = "BatchResult", Namespace = "https://example.com/operations")]
public BatchResult batchResult { get; set; }
}
[XmlRoot(ElementName = "BatchResult", Namespace = "https://example.com/operations")]
public class BatchResult
{
[XmlElement(ElementName = "FailureMessages", Namespace = "https://example.com/responses")]
public string failureMessages { get; set; }
[XmlElement(ElementName = "FailureType", Namespace = "https://example.com/responses")]
public string failureType { get; set; }
[XmlElement(ElementName = "ProcessedSuccessfully", Namespace = "https://example.com/responses")]
public Boolean processedSuccessfully { get; set; }
[XmlElement(ElementName = "SessionId", Namespace = "https://example.com/responses")]
public string sessionId { get; set; }
[XmlElement(ElementName = "TotalPages", Namespace = "https://example.com/responses")]
public int totalPages { get; set; }
}
}
you can use local-name() to ignore the namespace in xpath
//*[local-name()='Envelope']/*[local-name()='Body']/*[local-name()='BatchResponse']

Deserializing XML has null object

I have the following XML class structure:
[XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope
{
[XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public Body Body { get; set; }
}
[XmlRoot(ElementName = "Body")]
public class Body
{
[XmlElement(ElementName = "CustomerBundleMaintainConfirmation_sync_V1", Namespace = "http://sap.com/xi/SAPGlobal20/Global")]
public CustomerMaintainConfirmation CustomerMaintainConfirmation { get; set; }
}
[XmlRoot(ElementName = "CustomerBundleMaintainConfirmation_sync_V1")]
public class CustomerMaintainConfirmation
{
[XmlElement(ElementName = "Log")]
public Log Log { get; set; }
}
[XmlRoot(ElementName = "Log")]
public class Log
{
[XmlElement(ElementName = "MaximumLogItemSeverityCode")]
public string MaximumLogItemSeverityCode { get; set; }
[XmlElement(ElementName = "Item")]
public Item Item { get; set; }
}
[XmlRoot(ElementName = "Item")]
public class Item
{
[XmlElement(ElementName = "TypeID")]
public string TypeID { get; set; }
[XmlElement(ElementName = "CategoryCode")]
public string CategoryCode { get; set; }
[XmlElement(ElementName = "SeverityCode")]
public string SeverityCode { get; set; }
[XmlElement(ElementName = "Note")]
public string Note { get; set; }
}
And this is the XML I am working with:
<?xml version="1.0" encoding="UTF-8"?>
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:Header />
<soap-env:Body>
<n0:CustomerBundleMaintainConfirmation_sync_V1 xmlns:n0="http://sap.com/xi/SAPGlobal20/Global" xmlns:prx="urn:sap.com:proxy:LBK:/1SAI/TAE6F3228CC6D723FF1823E:804">
<Log>
<MaximumLogItemSeverityCode>3</MaximumLogItemSeverityCode>
<Item>
<TypeID>018</TypeID>
<CategoryCode>YES</CategoryCode>
<SeverityCode>0</SeverityCode>
<Note>TestNotes</Note>
</Item>
</Log>
</n0:CustomerBundleMaintainConfirmation_sync_V1>
</soap-env:Body>
</soap-env:Envelope>
When I attempt to deserialize this data into my classes, for whatever reason the "Log" class is null. Envelope, Body and CustomerMaintainConfirmation are all populated correctly. I see no reason why this is the case, but I've been staring at this for so long as this point I'm absolutely sure I am missing a mistake in and amongst my code somewhere.
This last piece of code is inside of the method that does the actual deserializing:
XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
using (StreamReader responseReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
return (Envelope)serializer.Deserialize(responseReader);
}
I'm still working through it, but if anyone could point out any issues they might see with what I've provided, please let me know.
It's a namespace inconsistency. It deserializes with this change:
[XmlRoot(ElementName = "CustomerBundleMaintainConfirmation_sync_V1")]
public class CustomerMaintainConfirmation
{
[XmlElement(ElementName = "Log", Namespace = "")]
public Log Log { get; set; }
}
You just miss the empty namespace indication on your element:
[XmlRoot(ElementName = "CustomerBundleMaintainConfirmation_sync_V1")]
public class CustomerMaintainConfirmation
{
[XmlElement(ElementName = "Log", Namespace = "")]
public Log Log { get; set; }
}
This is required, as apparently, your Log element has the empty namespace even though the CustomerBundleMaintainConfirmation_sync_V1 above has a nonempty one.
That's the only change in your code to make it work as shown in a fiddle.

How to deserialize xml parent root attribute

I have following XML Structure:
<ComputationData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" DataType="DimX">
<Mode Name="M1">
<Area>
<Point PointNumber="3" Speed="127" Power="1455" Value="-2" />
<Point PointNumber="2" Speed="127" Power="1396.8" Value="2" />
<Point PointNumber="3" Speed="101.6" Power="1164" Value="-2" />
</Area>
</Mode>
</ComputationData>
In below class structure I am not able to get the value of Datatype which is available in XMl root(ComputationData).
For following XML I have created following class structure:
[Serializable]
[XmlRoot("ComputationData")]
public class FullLoadPerformanceGrid
{
/// <summary>
/// Name of the performance data type of this grid, e.g. Bsfc, tEat...
/// </summary>
[XmlElement(ElementName = "ComputationData", Type =
typeof(PerformanceType))]
public PerformanceType DataType { get; set; }
/// <summary>
/// List of available <see cref="FullLoadPerformanceMode"/>
/// </summary>
[XmlElement("Mode", Type = typeof(FullLoadPerformanceMode))]
public List<FullLoadPerformanceMode> Modes { get; set; }
}
[Serializable]
[XmlRoot("ComputationData")]
public class PerformanceType
{
public int Id { get; set; }
[DataMember(Name = "DataType")]
[XmlAttribute("DataType")]
public string DataType { get; set; }
public int Type { get; set; }
}
Can anyone help me with the class structure that how should I define the PerformanceType(DataType)?
Try following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication3
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(FullLoadPerformanceGrid));
FullLoadPerformanceGrid load = (FullLoadPerformanceGrid)serializer.Deserialize(reader);
}
}
[XmlRoot(ElementName = "ComputationData", Namespace = "")]
public class FullLoadPerformanceGrid
{
[XmlAttribute("DataType", Namespace = "")]
public string DataType { get; set; }
[XmlElement(ElementName = "Mode", Namespace = "")]
public Mode mode { get; set; }
}
[XmlRoot(ElementName = "Mode", Namespace = "" )]
public class Mode
{
[XmlAttribute("Name", Namespace = "")]
public string name { get; set; }
[XmlArray("Area", Namespace = "")]
[XmlArrayItem("Point", Namespace = "")]
public List<Point> points { get; set; }
}
[XmlRoot(ElementName = "Point", Namespace = "")]
public class Point
{
[XmlAttribute("PointNumber", Namespace = "")]
public int pointNumber { get; set; }
[XmlAttribute("Speed", Namespace = "")]
public decimal speed { get; set; }
[XmlAttribute("Power", Namespace = "")]
public decimal power { get; set; }
[XmlAttribute("Value", Namespace = "")]
public int value { get; set; }
}
}

C# Xml select node contains node contains innertext contains text

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?
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....
Select Nodes Using XPath Navigation is what you needed.
and XPath Example

Categories

Resources