Thursday, April 10, 2025

Read XMl File data With namespace and without namespace

 

Suppose xml files is avaialble like this with namespace

<?xml version="1.0" encoding="UTF-8"?> <Document xmlns="http://abcd.org/onmags/schema"> <AuthReq> <MyData> Hi How are you </MyData> </AuthReq> </Document>

.Net Sample Code looks like this

#region With Namespace XmlDocument doc = new XmlDocument(); doc.Load(Server.MapPath("/Sample/request.xml")); var nms = new XmlNamespaceManager(doc.NameTable); nms.AddNamespace("nm", "http://abcd.org/onmags/schema"); XmlNode node = doc.DocumentElement.SelectSingleNode("/nm:Document/nm:AuthReq", nms); //XmlNode node = doc.DocumentElement.SelectSingleNode("/Document/AuthReq"); string request = node.InnerText.ToString(); #endregion =================================================================

Suppose xml files is avaialble like this without namespace

<?xml version="1.0" encoding="UTF-8"?> <Document> <AuthReq> <MyData> Hi How are you </MyData> </AuthReq> </Document>

.Net Sample Code looks like this

#region With Namespace XmlDocument doc = new XmlDocument(); doc.Load(Server.MapPath("/Sample/request.xml")); XmlNode node = doc.DocumentElement.SelectSingleNode("/Document/AuthReq"); string request = node.InnerText.ToString(); #endregion