Can't quite parse XML correctly - please help

R

Ronald S. Cook

Hi,

I have the following code that reads an XML document. I want it to delete
nodes where the date is less than today. It *almost* works (please see the
output below the code). How can I make the <Data /> element completely
disappear for those first 3 nodes that were earlier than the current date?

XmlDocument doc = new XmlDocument();
doc.Load(ConfigurationManager.AppSettings["keyXML"]);

XmlNode nodVDIData = doc.FirstChild;
XmlNode nodData = nodVDIData.FirstChild;

while (nodData != null)
{
XmlNode element = nodData.SelectSingleNode("TimeEventStart");
string s2 = element.InnerText;
DateTime dtm = XmlConvert.ToDateTime(s2,
XmlDateTimeSerializationMode.Local);

if (dtm < DateTime.Now)
nodData.RemoveAll();

nodData = nodData.NextSibling;
}

context.Response.ContentType = "text/xml";
XmlWriter writer = XmlWriter.Create(context.Response.OutputStream);
writer.WriteRaw(nodVDIData.OuterXml);

---------------------------------

<?xml version="1.0" encoding="utf-8" ?>
- <VDIData>
<Data />
<Data />
<Data />
- <Data>
<TimeEventStart>2006-05-06T20:00:00.0000000-07:00</TimeEventStart>
<TimeEventEnd>2006-03-06T22:00:00.0000000-07:00</TimeEventEnd>
<GroupName>DDD</GroupName>
</Data>
- <Data>
<TimeEventStart>2006-05-06T13:00:00.0000000-07:00</TimeEventStart>
<TimeEventEnd>2006-03-06T15:00:00.0000000-07:00</TimeEventEnd>
<GroupName>EEE</GroupName>
</Data>
</VDIData>
 
C

Chris Dunaway

Try deleting the node from the VDIData node:

(watch for typos)

nodVDIData.RemoveChild(nodData);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top