Don't use Maciej approach, you will definitely get an
OutOfMemoryException
exception if you are expecting an XML file of +5GB.
Also that second approach in which you are using the
XmlTextReader
is a right way to go, that is the only way you can read XML without loading an entire document at once, but you need to tweak it a bit.
First note that it's recommended to use
XmlReader.Create
method instead of instantiating an
XmlTextReader
, second when you are reading an
"issue"
element you want to use
ReadInnerXml
instead of
ReadOuterXml
, third you don't want to use
Skip
instead you want to continue with reading the sibling
"issue"
elements.
Try this:
protected void Button2_Click(object sender, EventArgs e)
{
string currentIssueNode = null;
XmlReaderSettings settings = new XmlReaderSettings() { IgnoreWhitespace = true };
using (var reader = XmlReader.Create(Server.MapPath(@"export_sample.xml"), settings))
{
string fieldvalue = string.Format("<fieldvalue id=\"fld1\">{0}</fieldvalue>", IDTextBox.Text);
if (reader.ReadToFollowing("issue"))
{
do
{
currentIssueNode = reader.ReadInnerXml();
if (currentIssueNode.Contains(fieldvalue))
break;
else
currentIssueNode = null;
} while (reader.ReadToNextSibling("issue"));
}
}
if (!string.IsNullOrEmpty(currentIssueNode))
Status.Text = currentIssueNode;
else
Status.Text = "ID does not Exists";
}