Вход XML. Исправлено пространство имен ..
<?xml version="1.0" encoding="utf-8" ?>
<Body xmlns:i="http://tempuri.org/body">
<Address>
<Segment i:type="AddressSegment">
<Id>9999</Id>
</Segment>
</Address>
<Segmentation>
<Segment xmlns:d4p1="http://tempuri.org/foo" i:type="d4p1:FooSegment">
<d4p1:Id>63</d4p1:Id>
</Segment>
<Segment xmlns:d4p1="http://tempuri.org/bar" i:type="d4p1:BarSegment">
<d4p1:Id>159</d4p1:Id>
</Segment>
<Segment i:type="BasicSegment">
<Id>10</Id>
</Segment>
</Segmentation>
</Body>
Создан класс модели для FooSegment & BooSegment с использованием xmltocsharp
[XmlRoot(ElementName = "Segment")]
public class FooSegment
{
[XmlElement(ElementName = "Id", Namespace = "http://tempuri.org/foo")]
public List<string> Id { get; set; }
[XmlAttribute(AttributeName = "d4p1", Namespace = "http://www.w3.org/2000/xmlns/")]
public string D4p1 { get; set; }
[XmlAttribute(AttributeName = "type", Namespace = "http://tempuri.org/body")]
public string Type { get; set; }
}
[XmlRoot(ElementName = "Segment")]
public class BarSegment
{
[XmlElement(ElementName = "Id", Namespace = "http://tempuri.org/bar")]
public List<string> Id { get; set; }
[XmlAttribute(AttributeName = "d4p1", Namespace = "http://www.w3.org/2000/xmlns/")]
public string D4p1 { get; set; }
[XmlAttribute(AttributeName = "type", Namespace = "http://tempuri.org/body")]
public string Type { get; set; }
}
Использовал эту модель для десериализации с использованием XmlSerializer
XDocument xml = XDocument.Load("1.xml");
XNamespace ns = "http://tempuri.org/body";
string[] values = new string[] { "d4p1:FooSegment", "d4p1:BarSegment" };
var result = xml.Root.DescendantsAndSelf("Segment")
.Where(r => values.Contains((string)r.Attribute(ns + "type").Value));
foreach (var nodeValue in result)
{
if(nodeValue.Attribute(ns + "type").Value.ToString() == "d4p1:FooSegment")
{
var fooObject = (FooSegment)new XmlSerializer(typeof(FooSegment)).Deserialize(new StringReader(nodeValue.ToString()));
Console.WriteLine($"{fooObject.Id[0]}");
}
else
{
var barObject = (BarSegment)new XmlSerializer(typeof(BarSegment)).Deserialize(new StringReader(nodeValue.ToString()));
Console.WriteLine($"{barObject.Id[0]}");
}
}
Выход
63
159