I have an xml file. Some nodes are fine. Some are invalid. And the point is to take the value from each node and save it into its corresponding property in the program. ill make up an example:
<Data>
<InvalidNode badAttribute=fml> 23 <InvalidNode/>
<ValidNode1> 232 <ValidNode1>
<ValidNode2 randomAttribute="yolo"> 333 <ValidNode2/>
<Data/>
So the second one is invalid because it should be badAttribute="fml"
Now, I have a reader that reads the xml. But at the moment, once it gets to an invalid node, it can't read past it. So the Nodes after it never get read! I am trying to work out how it could just skip past the invalid node and then continue reading through the rest of the xml file. Ill give u a simplified version of the method:
var reader = XmlReader.Create(xmlSource);
if (reader.ReadToDescendant("Data"))
{
while (reader.Read())
{
while (myreader.IsStartElement() && !myreader.IsEmptyElement)
{
var property = GetPropertyIfExists(reader.Name)
if (property != null)
{
myreader.ReadSubtree().Read();
try
{
var node = XNode.ReadFrom(myreader) as XElement;
//Do other things if its valid....
}
catch(Exception e)
{
//Skip this node somehow and go to next one??
//tried reader.skip() and reader.Read(). nothing works :( :(
}
}
else
{
myreader.Skip();
break;
}
}
}
}
At the moment, it throws an exception there. But after that, if I try to reader.Read(), it returns no. So i can't really read past it. I have literally no idea how to fix this :( any help would be sooooo great. thanks