I have to be able to parse both these xml-s:
<!-- Case 1 -->
<root xmlns="http://www.xml.namespace/111111">
<dataObject id="Id1" name="Name1" xmlns="http://www.xml.namespace/222222" />
</root>
<!-- Case 2 -->
<root xmlns:myNs="http://www.xml.namespace/222222">
<myNs:dataObject myNs:id="Id1" myNs:name="Name1" />
</root>
I have to parse the <dataObject> (via XmlSerializer) into a C# model class which has this:
[XmlAttribute(AttributeName = "id", Namespace="http://www.xml.namespace/222222")]
public string Id { get; set; }
But it only works for Case 2, does not work for Case 1.
So I examined Case 1 with this code:
var xmlString = @"<dataObject id=""Id1"" name=""Name1"" xmlns=""http://www.xml.namespace/222222"" />";
var xElement = XElement.Parse(xmlString);
var idAttribute = xElement.FirstAttribute;
Console.WriteLine(idAttribute.Name.Namespace); // ""
Console.WriteLine(xElement.Name.Namespace); // "http://www.xml.namespace/222222"
Here the XElement thinks it has a namespace, while the XAttributes think they don't have any.
So the XAttributes are somehow not aware of the sibling xmlns xml namespace declaration attribute.
Is this a bug, or am I doing something wrong?