Select an particular node (with colon) from xml file and bind it in model class

597 Views Asked by At

I need to read an xml file tag which has colon in it and bind it into model class

My xml file

<?xml version="1.0" encoding="utf-8"?>
<feed xml:base=""
    xmlns=""
    xmlns:d=""
    xmlns:m="">
    <id>my url</id>
    <title type="text">ItemList</title>
    <updated>2019-05-07T14:18:08Z</updated>
    <link rel="self" title="ItemList" href="ItemList" />
    <entry m:etag="">
        <id></id>
        <category term="NAV.ItemList" scheme="" />
        <link rel="edit" title="ItemList" href="" />
        <title />
        <updated>2019-05-07T14:18:08Z</updated>
        <author>
            <name />
        </author>
        <content type="application/xml">
            <m:properties>
                <d:No>LSU-458</d:No>
                <d:Description>speaker</d:Description>
                <d:Type>Inventory</d:Type>
            </m:properties>
        </content>
    </entry>
</feed>

My c# code

 List<CustomerModel> customers = new List<CustomerModel>();
 doc.Load(Server.MapPath("~/oWtMRUR8_.xml"));
 foreach (XmlNode node in 
 doc.SelectNodes("/feed/id/title/updated/link/mproperties"))
 {
    customers.Add(new CustomerModel
    {
        No = int.Parse(node["d:No"].InnerText),
        Description = node["d:Description"].InnerText,
        Type = node["d:Type"].InnerText
    });
 }
 return View(customers);

I need to bind only the d:no,d:description,d:type in my model class using foreach loop,these tags are with colon so I am not able to extract the contents in the tag and namespace was mentioned as an option(but didn't worked). I am new to this and can't able to find the right method, hope anyone helps.

My expected result:

                No =LSU-458 ,
                Description = speaker,
                Type  = Inventory
2

There are 2 best solutions below

10
iakobski On

Firstly, the XML is not valid, the namespace declarations need a name, for example

<feed xml:base="""" xmlns="""" xmlns:d=""d"" xmlns:m=""m"">

Then you can loop through the properties like this:

var root = doc.SelectSingleNode("/feed/entry/content");
foreach (XmlNode node in root.ChildNodes)
{
    customers.Add(new CustomerModel
    {
        No = node["d:No"].InnerText,
        Description = node["d:Description"].InnerText,
        Type = node["d:Type"].InnerText
    });
}

And finally, you cannot int.Parse() the d:No because it's a string.

0
Gokul On

Hi all I got the answer and I am posting it, hope it might help someone in future:

XmlNamespaceManager namespaces = new
XmlNamespaceManager(doc.NameTable);
namespaces.AddNamespace("m", "our_url_link");
XmlNodeList nodemsg = doc.SelectNodes("//m:properties", namespaces);
foreach (XmlNode xnz in nodemsg)
{
    customers.Add(new CustomerModel
    {
        //item.Element("d:No").Value
        No = xnz["d:No"].InnerText,
        Description = xnz["d:Description"].InnerText,
        Type = xnz["d:Type"].InnerText
    });
}