XPath does not work in xml created using DataContractSerializer


                                                                                            I have a datacontract object and I can serialize it successfully in xml using the DataContractSerializer, but when I tried to access it once from node using XPath, it returns null. I cannot figure out why this is happening.

This is what I still have.

namespace DataContractLibrary
{
    [DataContract]
    public class Person
    {
        [DataMember]
        public string FirstName { get; set; }

        [DataMember]
        public string LastName { get; set; }

        [DataMember]
        public int Age { get; set; }
    }
}

static void Main(string[] args)
{
    Person dataContractObject = new Person();
    dataContractObject.Age = 34;
    dataContractObject.FirstName = "SomeFirstName";
    dataContractObject.LastName = "SomeLastName";

    var dataSerializer = new DataContractSerializer(dataContractObject.GetType());

    XmlWriterSettings xmlSettings = new XmlWriterSettings { Indent = true, Encoding = Encoding.UTF8, OmitXmlDeclaration = true };
    using (var xmlWriter = XmlWriter.Create("person.xml", xmlSettings))
    {
        dataSerializer.WriteObject(xmlWriter, dataContractObject);
    }

    XmlDocument document = new XmlDocument();
    document.Load("person.xml");

    XmlNamespaceManager namesapceManager = new XmlNamespaceManager(document.NameTable);
    namesapceManager.AddNamespace("", document.DocumentElement.NamespaceURI);

    XmlNode firstName = document.SelectSingleNode("//FirstName", namesapceManager);

    if (firstName==null)
    {
        Console.WriteLine("Count not find the node.");
    }

    Console.ReadLine();
}

Can someone tell me what went wrong for me? Your help would be greatly appreciated.

+3
source share
1 answer

You ignore the XML namespace that fits into serialized XML:

<Person xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns="http://schemas.datacontract.org/2004/07/DataContractLibrary">
   <Age>34</Age>
   <FirstName>SomeFirstName</FirstName>
   <LastName>SomeLastName</LastName>
</Person>

So, in your code, you need to reference this namespace:

XmlNamespaceManager namespaceManager = new XmlNamespaceManager(document.NameTable);
namespaceManager.AddNamespace("ns", document.DocumentElement.NamespaceURI);

XPath, :

XmlNode firstName = document.SelectSingleNode("//ns:FirstName", namespaceManager);

if (firstName == null)
{
   Console.WriteLine("Could not find the node.");
}
else
{
   Console.WriteLine("First Name is: {0}", firstName.InnerText);
}

- .

+5

All Articles