Bob van der Valk
Bob van der Valk

Reputation: 23

Loading XML file

I am loading redirects from a xml file.

The XML file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfRedirectModel xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <RedirectModel>
    <Time>0001-01-01T00:00:00</Time>
    <oldUrl>/301/</oldUrl>
    <newUrl>/alloy-track/</newUrl>
    <type>1</type>
    <Id>6d278d45-aab1-4fa2-953f-f03963a29ff8</Id>
  </RedirectModel>
</ArrayOfRedirectModel>

And where I read the xml file the code looks like this.

XmlDocument xDoc = new XmlDocument();
string mappath = HttpContext.Current.Server.MapPath("~/ClientResources/SeoCache");
xDoc.Load(mappath + "//cache.xml");
XmlNodeList xmlSelectedNodes = xDoc.SelectNodes("RedirectModel");   

foreach (XmlNode node in xmlSelectedNodes)
{

But my "xmlSelectedNodes" stays "null".

I want to get the "oldUrl" and the "newUrl" in a string variable so I can use it.

Does anyone know what I am doing wrong?

Upvotes: 0

Views: 60

Answers (2)

Ravindra patankar
Ravindra patankar

Reputation: 113

Yes its should be

XmlNodeList xmlSelectedNodes = xDoc.SelectNodes("//ArrayOfRedirectModel/RedirectModel");

Upvotes: 0

Lloyd
Lloyd

Reputation: 29668

You're missing the root element completely, try:

XmlNodeList xmlSelectedNodes = xDoc.SelectNodes("//ArrayOfRedirectModel/RedirectModel"); 

Your original code would also work if you selected nodes from the root node itself:

XmlNodeList xmlSelectedNodes = xDoc.DocumentElement.SelectNodes("RedirectModel"); 

Upvotes: 3

Related Questions