XmlTextReader C# Problems With Reading and Writing - c#

So, I am trying to make a "Recent Files" feature in my WPF application by use of C#'s XML class.
Basically, the program needs to read each node and get the URI of file to generate menu item.
Problem is, r.Value doesn't give me a string to work with and I'm starting to question whether or not this code is going to work.
XmlTextReader r = new XmlTextReader(this.PathToSpecialFolder + #"\" + Application.CompanyName + #"\" + Application.ProductName + #"\Recent.xml");
int count = 0;
while (r.Read())
{
switch (r.NodeType)
{
case XmlNodeType.Element:
if (r.Name == "Path")
{
count++;
if (count <= 5)
{
MenuItem m = new MenuItem() { Header = r.Value };
openRecentMenuItem.Items.Add(m);
//r.Value populates nothing
}
}
break;
}
}
r.Close();
On a related topic, I'm also trying to get it to where the XML file will only store five nodes. I want to...
Get number of nodes (which I have already accomplished)
Check whether new node + node count > 5 and, if so, prepend new node and remove last node. Otherwise, just add new node
How do you remove the last node? Here is my XML file structure:
<?xml version="1.0" encoding="utf-8"?>
<RecentFiles>
<File>
<Path>Path goes here.</Path>
</File>
</RecentFiles>
UPDATE:
The code below solves my issue with reading (as per advice of John), however, I still cannot figure out how to remove the last node when writing. I prefer not to use Linq in this case, so a solution using XmlReader is preferred, thanks.
XmlReader x = XmlReader.Create(this.PathToSpecialFolder + #"\" + Application.CompanyName + #"\" + Application.ProductName + #"\Recent.xml");
int c = 0;
while (x.Read())
{
if (x.NodeType == XmlNodeType.Element && x.Name == "Path")
{
c++;
if (c <= 10)
{
MenuItem m = new MenuItem() { Header = x.ReadInnerXml() };
m.Click += delegate
{
};
openRecentMenuItem.Items.Add(m);
}
}
}
x.Close();
}

Related

How to override a specific line in a text file in c#

I have the following situation. I am creating a quiz game in c# visual studio and want to create a Register and Login forms. When a user registers a new account the text file will store their username and password and will set the high score to 0. Each line in the text file looks like that: username;password;highscore. ';' is the delimiter. I have created a new project to create a practice login/ register form. Here is my code for the register form:
private void btnRegister_Click(object sender, EventArgs e)
{
if (txtPassword.Text == txtConfirmPassword.Text)
{
string newAccount = txtName.Text + ";" + txtConfirmPassword.Text + ";" + "0";
TextWriter account = new StreamWriter("../../TextFile/LogonDetails.txt", true);
account.WriteLine(newAccount);
account.Close();
MessageBox.Show("Account created");
}
and here is my code for the login form
string line = "";
StreamReader myReader = new StreamReader("../../TextFile/LogonDetails.txt");
string[] accounts = new string[900000]; int value = 0;
while ((line=myReader.ReadLine()) != null)
{
string[] data = line.Split(';');
if ((data[0] == txtLoginName.Text) && (data[1] == txtLoginPassword.Text) && (int.Parse(data[2]) > int.Parse(txtScore.Text)))
{
value = 1;
break;
}
if ((data[0] == txtLoginName.Text) && (data[1] == txtLoginPassword.Text) && (int.Parse(data[2]) < int.Parse(txtScore.Text)))
{
value = 2;
break;
}
else
{
value = 3;
}
}
if (value == 1)
{
MessageBox.Show("Your score remains the same");
}
else if (value == 2)
{
string updatedAccount = txtLoginName.Text + ";" + txtLoginPassword.Text + ";" + txtScore;
TextWriter textAccounts = new StreamWriter("../../TextFile/LogonDetails.txt");
textAccounts.WriteLine(updatedAccount);
textAccounts.Close();
}
else if (value == 3)
{
MessageBox.Show("Account not found");
}
}
So my question is how can I override the line stored in the text file if in this case 'txtScore' is greater than data[2]? I have tried creating a new line each time the score is greater but that seems inefficient. Is there a way that i can override the line to change the score value? Any help is greatly appreciated
If the file is not that long you can do something like this:
String fileName = #"C:\LogonDetails.txt";
var data = File
.ReadLines(fileName)
.Select(line => line.Split(';'))
.Select(items => {
if ((items[0] == txtLoginName.Text) &&
(items[1] == txtLoginName.Text) &&
(int.Parse(items[2]) < int.Parse(txtScore.Text)))
items[2] = int.Parse(txtScore.Text);
return items;
})
.ToList(); // materialize in oreder to prevent file read/write collision
File.WriteAllLines(fileName, data);
There is no straightforward way to target a specific line in a text file to update the contents on that line. I would suggest storing your information in an XML (structured data) format; .NET already has the capabilities built in for reading and writing to specific nodes in an XML file.
If you don't want to do that, then my suggestion would be that you load all the lines from the text file into memory as instances of, e.g. a User class that has as properties your username, password, and score, and then write them all back out to your data file all at once with any updates to the scores.
By the way, it's generally not a good idea to store passwords in plain text, so I would hope you're at least employing a hashing algorithm.
Well, based on your question you know the line number, so do something like this:
var lines = File.ReadAllLines(#"path to file");
if (lines.Contains("1234"))
{
lines[Array.IndexOf(lines, "1234")] = "new york";
}
File.WriteAllLines(#"path to file", lines);
try like this.

Compare two XML files and save differences to a results file

I have two XML files that I need to compare for differences, the XML is very simple:
File 1:
<?xml version="1.0" encoding="utf-8"?>
<Feeds zone="my zone">
<Feed name="attribDump.json">ac1f07edc491a3d237cdfb1a17fc4551</Feed>
<Feed name="focus_GroupsKV.txt">0f9e0a14a4ffce6ff5065b6e088c1f84</Feed>
<Feed name="NAM_FORMATTED.csv">9e875496cdb072b5e54318d51295fdba</Feed>
<Feed name="BNP\activityTitles.txt">2d27c0f19b71b4b411bcb00011d3f8b0</Feed>
</Feeds>
and File 2:
<?xml version="1.0" encoding="utf-8"?>
<FeedsRequest version="1">
<Feeds zone="my zone">
<Feed name="attribDump.json">ac1f07edc491a3d237cdfb1a17fc4551</Feed>
<Feed name="focus_GroupsKV.txt">0f9e0a14a4ffce6ff5065b6e088c1f84</Feed>
<Feed name="BNP\activityTitles.txt">e54c5b851ee3ff3f43b10d24f2316431</Feed>
</Feeds>
</FeedsRequest>
File 1 is an inventory list of files on our file share and File 2 is used by a disconnected device that will need to be refreshed from File 1. The checks I need to make are 1) make sure all of the feeds in File 1 are in File 2 and 2) make sure any feeds that are found have the same hashCode(the long character string). Once the checks have been completed, I need to create a response file that has a list of all of the feeds and then an attribute on each one that designates ok (file was found and matched), missing (file wasn't found), or updated (file was found but it was an older version).
So basically the result file would look as such:
<?xml version="1.0" encoding="utf-8"?>
<FeedsResponse version="1">
<Feeds zone="my zone">
<Feed name="attribDump.json" status="ok">ac1f07edc491a3d237cdfb1a17fc4551</Feed>
<Feed name="focus_GroupsKV.txt" status="ok">0f9e0a14a4ffce6ff5065b6e088c1f84</Feed>
<Feed name="NAM_FORMATTED.csv" status="missing">afd2c620053ed4f85ab02b4cc5f7a2b2</Feed>
<Feed name="BNP\activityTitles.txt" status="updated">90805b851ee3ff3f43b10d24f2316431</Feed>
What I'm doing currently is looping through all of the files in File 1, then checking them against File 2 for differences. Where I'm stuck, been a while since I've worked with XML, is how to build out the response document.
FileInfo feedList = new FileInfo(_feedList);
FileInfo feedRequest = new FileInfo(_feedRequest);
// Load the documents
XmlDocument feedListXmlDoc = new XmlDocument();
feedListXmlDoc.Load(_feedList);
// Load the documents
XmlDocument feedRequestXmlDoc = new XmlDocument();
feedRequestXmlDoc.Load(_feedRequest);
//create response doc
XmlDocument feedResponseXmlDoc = new XmlDocument();
// Define a single node
XmlNode feedListNode;
XmlNode feedRequestNode;
// Get the root Xml element
XmlElement feedListRoot = feedListXmlDoc.DocumentElement;
XmlElement feedRequestRoot = feedRequestXmlDoc.DocumentElement;
// Get a list of all player names
XmlNodeList feedListXml = feedListRoot.GetElementsByTagName("Feed");
XmlNodeList feedRequestXml = feedRequestRoot.GetElementsByTagName("Feed");
// Create an XmlWriterSettings object with the correct options.
XmlWriter writer = null;
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = (" ");
settings.OmitXmlDeclaration = false;
// Create the XmlWriter object and write some content.
writer = XmlWriter.Create(_resultPath, settings);
writer.WriteStartElement("FeedsDiff");
// The compare algorithm
bool feedMatch = false;
int j = 0;
try
{
// loop through list of current feeds
for (int i = 0; i < feedListXml.Count; i++)
{
feedListNode = feedListXml.Item(i);
string feedListName = feedListNode.Attributes["name"].Value.ToString();
string feedListHash = feedListXml.Item(i).InnerText.ToString();
//check feed request list for a match
while (j < feedRequestXml.Count && feedMatch == false)
{
feedRequestNode = feedRequestXml.Item(j);
string feedRequestName = feedRequestNode.Attributes["name"].Value.ToString();
//checks to see if feed names match
if (feedListName == feedRequestName)
{
feedMatch = true;
string feedRequestHash = feedRequestXml.Item(j).InnerText.ToString();
//since we found the node, we can remove it from the request list
XmlNode node = feedRequestNode.ParentNode;
node.RemoveChild(feedRequestNode);
//checks to see if hash codes match
if (feedListHash == feedRequestHash)
{
//if name and code match, move to the next one
feedMatch = true;
//add 'status="ok"' attribute to the node
//feedResponseXmlDoc.ImportNode(feedRequestNode,false);
Debug.WriteLine(feedListName + " name and hash match");
j = 0;
}
else
{
feedMatch = true;
//feed has been updated since last device sync
//need to add status='update' attribute and append file to response
Debug.WriteLine(feedListName + " name matched but hash did not");
}
}
else
{
//names didn't match
//add status="missing" to the node
j++;
}
}
feedMatch = false;
}
// end Xml document
writer.WriteEndElement();
writer.Flush();
}
finally
{
if (writer != null)
writer.Close();
}
Right now I'm trying to instantiate the response doc before the loop and then just add the elements as they are found but I'm having a hard time finding a concise way to do it. Any help is appreciated.
Take a look at differ, from my open-source project CodeBlocks over on CodePlex, it was designed for situations such as this one. It is also available on Nuget as "differ"
I figured it out:
public void CompareXml(string _feedList, string _feedRequest, string _resultPath)
{
FileInfo feedList = new FileInfo(_feedList);
FileInfo feedRequest = new FileInfo(_feedRequest);
// Load the documents
XmlDocument feedListXmlDoc = new XmlDocument();
feedListXmlDoc.Load(_feedList);
// Load the documents
XmlDocument feedRequestXmlDoc = new XmlDocument();
feedRequestXmlDoc.Load(_feedRequest);
// Define a single node
XmlNode feedListNode;
XmlNode feedRequestNode;
// Get the root Xml element
XmlElement feedListRoot = feedListXmlDoc.DocumentElement;
XmlElement feedRequestRoot = feedRequestXmlDoc.DocumentElement;
// Get a list of feeds for the stored list and the request
XmlNodeList feedListXml = feedListRoot.GetElementsByTagName("Feed");
XmlNodeList feedRequestXml = feedRequestRoot.GetElementsByTagName("Feed");
bool feedLocated = false;
int j = 0;
try
{
// loop through list of current feeds
for (int i = 0; i < feedListXml.Count; i++)
{
feedListNode = feedListXml.Item(i);
//create status attribute
XmlAttribute attr = feedListXmlDoc.CreateAttribute("status");
string feedListName = feedListNode.Attributes["name"].Value.ToString();
string feedListHash = feedListXml.Item(i).InnerText.ToString();
//check feed request list for a match
while (j < feedRequestXml.Count && feedLocated == false)
{
feedRequestNode = feedRequestXml.Item(j);
string feedRequestName = feedRequestNode.Attributes["name"].Value.ToString();
//checks to see if feed names match
if (feedRequestName == feedListName)
{
string feedRequestHash = feedRequestXml.Item(j).InnerText.ToString();
//checks to see if hashCodes match
if (feedListHash == feedRequestHash)
{
//if name and code match, set status to ok
attr.Value = "ok";
Debug.WriteLine(feedListName + " name and hash match. Status: 'ok'");
}
else
{
//if hashCodes don't match, set status attribute to updated
attr.Value = "updated";
Debug.WriteLine(feedListName + " name matched but hash did not. Status: 'updated'");
}
feedListNode.Attributes.Append(attr);
feedLocated = true;
}
else
{
//names didn't match, checking to see if we're at the end of the request list
if (j + 1 == feedRequestXml.Count)
{
//file name wasn't found in the request list, set status attribute to missing
attr.Value = "missing";
feedListNode.Attributes.Append(attr);
feedLocated = true;
j = 0;
Debug.WriteLine("Reached the end of the file request list without a match. Status: 'missing'");
}
//file name wasn't located on this pass, move to next record
j++;
}
}
feedLocated = false;
}
}
finally
{
Debug.WriteLine("Result file has been written out at " + _resultPath);
}
feedListXmlDoc.Save(_resultPath);
}

When i read the xml file it reads duplicates of the node

When i try read my xml file it seems to reads it twice and im unsure on how to fix such an error. I thought i can have some sort of loop but im still lost. Any help would be appreciated ty. - It writes the xml file correctly but the duplicate occurs when it reads it.
String workingDir = Directory.GetCurrentDirectory();
XmlTextReader textReader = new XmlTextReader(workingDir + #"\xmldoc.xml");
Console.WriteLine("BaseURI:" + textReader.BaseURI);
textReader.Read();
while (textReader.Read())
{
if (textReader.Name == "test")
{
textReader.Read();
XmlNodeType nType = textReader.NodeType;
if (nType == XmlNodeType.Text)
{
// label.Text = textReader.Value.ToString();
Label l = new Label();
System.Drawing.Point l1 = new System.Drawing.Point(15, 13 + a);
l.Location = l1;
l.Text = textReader.Value.ToString();
a += 20;
}
What makes you think some entries are read twice? If it is the case, also check if this method is not called twice (shift + F12 in Visual Studio to find usage).
Also, it seems that the piece of code you joined here is not complete (no declaration of variable 'a'). Do you have some code executed under the if (textReader.Name == "test") that would do the same operations?
I have no idea what you are REALLY trying to do, and no visibility of you XML, but here is roughly how I would do it.
Notes:
I'm using an XmlReader <- Better IMHO (base class, so there's less waffle)
I'm using "reader.Value" without the ToString() as it's already a string type.
I changed it to be a switch as I think they are cleaner, but there are A LOT of XmlNodeTypes out there and you don't want to have to if/else to much!!!
Code:
XmlReader reader = XmlReader.Create("books.xml");
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an element.
//DO NOTHING
break;
case XmlNodeType.Text: //Display the text in each element.
//label.Text = reader.Value;
Label l = new Label();
System.Drawing.Point l1 = new System.Drawing.Point(15, 13 + a);
l.Location = l1;
l.Text = reader.Value;
a += 20;
break;
case XmlNodeType.EndElement: //Display the end of the element.
//DO NOTHING
break;
}
}

how to read and change the xml file in C#

Thank you very much for reading my question.
and this is my xml file. (for node Songs, many childNodes named Song)
<?xml version="1.0" encoding="utf-8" ?>
<xmlData>
<version>1.0</version>
<Songs>
<Song>
<artist>mic</artist>
<track>2</track>
<column>happy</column>
<date>14</date>
</Song>
<Song>
<artist>cool</artist>
<track>2</track>
<column>work</column>
<date>4</date>
</Song>
</Songs>
</xmlData>
reading xml, i use the following code:
XmlDocument doc = new XmlDocument();
doc.Load(xmlFilePath);
XmlNode versionNode = doc.SelectSingleNode(#"/xmlData/version");
Console.WriteLine(versionNode.Name + ":\t" + versionNode.InnerText);
XmlNode SongsNode = doc.SelectSingleNode(#"/xmlData/Songs");
Console.WriteLine(SongsNode.Name + "\n");
XmlDocument docSub = new XmlDocument();
docSub.LoadXml(SongsNode.OuterXml);
XmlNodeList SongList = docSub.SelectNodes(#"/Songs/Song");
if (SongList != null)
{
foreach (XmlNode SongNode in SongList)
{
XmlNode artistDetail = SongNode.SelectSingleNode("artist");
Console.WriteLine(artistDetail.Name + "\t: " + artistDetail.InnerText);
XmlNode trackDetail = SongNode.SelectSingleNode("track");
Console.WriteLine(trackDetail.Name + "\t: " + trackDetail.InnerText);
XmlNode columnDetail = SongNode.SelectSingleNode("column");
Console.WriteLine(columnDetail.Name + "\t: " + columnDetail.InnerText);
XmlNode dateDetail = SongNode.SelectSingleNode("date");
Console.WriteLine(dateDetail.Name + "\t: " + dateDetail.InnerText + "\n");
}
}
it seems working.
but how can i write the change to xml file?
maybe, i will change some childNode in Song, and may delete the whole chindNode by artist keyword.
is it possible such as this function
bool DeleteSongByArtist(string sArtist);
bool ChangeNodeInSong(string sArtist, string sNodeName, string value);
because the "Reading solution is "XmlDucoment", so it is better if "changing solution" by using "XmlDocument"
but, if you have better idea to read and change the xml file, please give me the sample code... and please don't write a name of solution such as "Ling to xml"...acutally, i do many testes, but failed.
Welcome to Stackoverflow!
You can change the nodes simply by setting a new .Value or in your case .InnerText.
Sample
// change the node
trackDetail.InnerText = "NewValue"
// save the document
doc.Save(xmlFilePath);
More Information
How To: Modify an Existing Xml File
MSDN - XmlDocument.Save Method
You need to use an XmlWriter. The easiest way to do it would be something like this...
using(XmlWriter writer = new XmlWriter(textWriter))
{
doc.WriteTo(writer);
}
Where textWriter is your initialized Text Writer.
Actually, forget that... the easiest way is to call...
doc.Save(xmlFilePath);
To delete an artist by artist name add the following method:
bool DeleteSongByArtist(XmlDocument doc, string artistName)
{
XmlNodeList SongList = doc.SelectNodes(#"/Songs/Song");
if (SongList != null)
{
for (int i = SongList.Count - 1; i >= 0; i--)
{
if (SongList[i]["artist"].InnerText == artistName && SongList[i].ParentNode != null)
{
SongList[i].ParentNode.RemoveChild(SongList[i]);
}
}
}
}
You probably want to clean it up a bit more to be more resilient. When you call it, change your initial code to be like this. Don't create the subDocument as you want to work with the entire XmlDocument.
XmlDocument doc = new XmlDocument();
doc.Load(xmlFilePath);
XmlNode versionNode = doc.SelectSingleNode(#"/xmlData/version");
Console.WriteLine(versionNode.Name + ":\t" + versionNode.InnerText);
XmlNode SongsNode = doc.SelectSingleNode(#"/xmlData/Songs");
Console.WriteLine(SongsNode.Name + "\n");
XmlNodeList SongList = doc.SelectNodes(#"/Songs/Song");
if (SongList != null)
{
foreach (XmlNode SongNode in SongList)
{
XmlNode artistDetail = SongNode.SelectSingleNode("artist");
Console.WriteLine(artistDetail.Name + "\t: " + artistDetail.InnerText);
XmlNode trackDetail = SongNode.SelectSingleNode("track");
Console.WriteLine(trackDetail.Name + "\t: " + trackDetail.InnerText);
XmlNode columnDetail = SongNode.SelectSingleNode("column");
Console.WriteLine(columnDetail.Name + "\t: " + columnDetail.InnerText);
XmlNode dateDetail = SongNode.SelectSingleNode("date");
Console.WriteLine(dateDetail.Name + "\t: " + dateDetail.InnerText + "\n");
}
}
You aren't able to save your changes because you made changes to an entirely new document!
You likely meant to do the following:
XmlNode SongsNode = doc.SelectSingleNode(#"/xmlData/Songs");
Console.WriteLine(SongsNode.Name + "\n");
// Don't make a new XmlDocument here! Use your existing one
XmlNodeList SongList = SongsNode.SelectNodes(#"/Song");
At this point SongList is still living inside doc. Now when you call:
doc.Save(xmlFilePath);
Your changes will be saved as you intended.
If you're looking to delete nodes that match certain criteria:
// Use XPath to find the matching node
XmlNode song = SongsNode.SelectSingleNode(#"/Song[artist='" + artist + "']");
// Remove it from its Parent
SongsNode.RemoveChild(song);
If you're looking to add a new node:
// Create the new nodes using doc
XmlNode newSong = doc.CreateElement("Song");
XmlNode artist = doc.CreateElement("artist");
artist.InnerText = "Hello";
// Begin the painstaking process of creation/appending
newSong.AppendChild(artist);
// rinse...repeat...
// Finally add the new song to the SongsNode
SongsNode.AppendChild(newSong);
You could do
XmlNodeList SongList = doc.SelectNodes(#"//Songs/Song");
The // tells it to select the Songs node anywhere in document. This is better than
doc.SelectNodes(#"/document/level1/music/Songs")
Note that the above statement is oviously not for your xml, but to prove a point about //
Using // removes the need for your docSub document and SongsNode element.
To add then delete a song, just use the following
XmlDocument doc = new XmlDocument();
XmlElement ea = doc.SelectSingleNode("//songs");
XmlElement el = doc.CreateElement("song");
XmlElement er;
ea.AppendChild(el);
//doing my work with ea
//you could use innerxml.
el.InnerXml = "<artist>Judas Priest</artist><track>7</track><column>good</column><date>1</date>";
//or you can treat each node as above
er = doc.CreateElement("Name");
el.AppendChild(er);
er.InnerText = "The Ripper";
//but you don't nead this song any more?
ea.RemoveChild(el);
//so it's gone.
And thats all there is to it.
please don't write a name of solution such as "Ling to xml"...acutally, i do many testes, but failed.
Still I think, this is a very good time to start to use Linq2Xml. If you don't like it, just ignore.
XDocument xDoc = XDocument.Load(new StringReader(xml));
//Load Songs
var songs = xDoc.Descendants("Song")
.Select(s => new
{
Artist = s.Element("artist").Value,
Track = s.Element("track").Value,
Column = s.Element("column").Value,
Date = s.Element("date").Value,
})
.ToArray();
//Delete Songs
string songByArtist="mic";
xDoc.Descendants("Song")
.Where(s => s.Element("artist").Value == songByArtist)
.Remove();
string newXml = xDoc.ToString();

Method Hangs on for loop and won't continue

I'm using html agility pack to parse several text files that I load. I then save the data that I parse out into a string list for further processing. However, when I use this method, it never hits the line:
MessageBox.Show("test");
Additionally, if I include any other code following this method, none of it is triggered.
Does anyone have any suggestions as to my error?
The entire method is included below:
private void ParseOutput()
{
nodeDupList = new List<string>();
StreamWriter OurStream;
OurStream = File.CreateText(dir + #"\CombinedPages.txt");
OurStream.Close();
for (int crawl = 1; crawl <= crawlPages.Length; crawl++)
{
var web = new HtmlWeb();
var doc = web.Load(dir + #"\Pages" + crawl.ToString() + ".txt");
var nodeCount = doc.DocumentNode.SelectNodes(#"/html[1]/body[1]/div[1]/table[3]/tbody[1]/tr[td/#class=""style_23""]");
int nCount = nodeCount.Count;
for (int a = 3; a <= nCount; a++)
{
var specContent = doc.DocumentNode.SelectNodes(#"/html[1]/body[1]/div[1]/table[3]/tbody[1]/tr[" + a + #"]/td[3]/div[contains(#class,'style_24')]");
foreach (HtmlNode node in specContent)
{
nodeDupList.Add(node.InnerText + ".d");
}
}
}
MessageBox.Show("test");
}
I've created a crawler to save multiple html pages to text and parse them separately using this method.
I'm just using MessageBox to show that it won't continue following the "for loop". I've called multiple methods in my solution and it won't iterate through them.
The application is a Win Forms Application targeted at .Net Framework 4.
Edit:
Thanks for the help.
I realized after rerunning it through the debugger that it was crashing at times on the loop
for (int a = 3; a <= nCount; a++)
{
var specContent = doc.DocumentNode.SelectNodes(#"/html[1]/body[1]/div[1]/table[3]/tbody[1]/tr[" + a + #"]/td[3]/div[contains(#class,'style_24')]");
foreach (HtmlNode node in specContent)
{
nodeDupList.Add(node.InnerText + ".d");
}
}
when the var specContent was null.
There was no exception generated; the method just ended.
As the website is dynamic that I was crawling it rarely returned null but on several instances it had and this happened.
The solution, for anyone who might need this is to check if
for (int a = 3; a <= nCount; a++)
{
var specContent = doc.DocumentNode.SelectNodes(#"/html[1]/body[1]/div[1]/table[3]/tbody[1]/tr[" + a + #"]/td[3]/div[contains(#class,'style_24')]");
if(specContent !=null) //added this check for null
{
foreach (HtmlNode node in specContent)
{
nodeDupList.Add(node.InnerText + ".d");
}
}
}
I also could have used a try{} catch{} block to output the error if needed

Categories

Resources