LINQ - Specified elements not being removed from XML file - c#

I have an XML file:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--QTabs Data Storage-->
<SyncTimes>
<LastSyncTime>
<id>1</id>
<SyncTime>3/31/2015 2:03:28 PM</SyncTime>
</LastSyncTime>
<LastSyncTime>
<id>2</id>
<SyncTime>3/31/2015 2:14:24 PM</SyncTime>
</LastSyncTime>
<LastSyncTime>
<id>3</id>
<SyncTime>3/31/2015 2:14:25 PM</SyncTime>
</LastSyncTime>
<LastSyncTime>
<id>4</id>
<SyncTime>3/31/2015 2:14:26 PM</SyncTime>
</LastSyncTime>
</SyncTimes>
All of the above times are earlier today I want to delete all LastSyncTime records before the current time (DateTime.Now):
public async void deleteArchivedSyncs()
{
var xElement = (from element in XMLDocObject.Elements("LastSyncTime")
where Convert.ToDateTime(element.Element("SyncTime").Value) < DateTime.Now
select element);
xElement.Remove();
storageFile = await storageFolder.GetFileAsync(Settings.xmlFile);
using (Stream fileStream = await storageFile.OpenStreamForWriteAsync())
{
XMLDocObject.Save(fileStream);
}
}
This being run does not effect the XML page. The desired elements are not being removed. What am I doing wrong?

This issue here appears to be that the only way to delete a child, is to have the parent do the deletion, as in:
class Program
{
public static void Main(params string[] args)
{
// test.xml contains OPs example content.
var xdoc = XDocument.Load(#"c:\temp\test.xml");
xdoc.Descendants("LastSyncTime")
.Where(e => Convert.ToDateTime(e.Element("SyncTime").Value) < DateTime.Now)
.Remove();
Console.WriteLine(xdoc);
xdoc.Save(#"c:\temp\test_filtered.xml");
}
}
This generates the following output:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--QTabs Data Storage-->
<SyncTimes />
I.e. an empty root, which is to be expected, given that all dates are smaller than DateTime.Now.

#DavidTunnell what is your root xml element that contains everything you need? in example:
//let's call the variable you use as Xdocument doc.
XmlNodeList nodes = doc.SelectNodes("LastSyncTime");
for (int i = nodes.Count - 1; i >= 0; i--)
{
nodes[i].ParentNode.RemoveChild(nodes[i]);
}
doc.Save(path);
This is how i had used hope it helps.

Related

Change ALL Tag Name to Lowercase inside XMLDocument

I need to change ALL the Tag name to Lowercase but leave the InnerText or Value as it is. I just found the thread showing how to change the casing for the entire document but NOT just for the Tag names.
Code
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlDoc.OuterXml.ToLower());
Original.xml
<?xml version="1.0" encoding="utf-8" ?>
<ROOT>
<InNeRtAg>SoMe TeXt</InNeRtAg>
<NeStEdElEmEnT>
<InNeRnEsTiNg>SoMe TeXt</InNeRnEsTiNg>
</NeStEdElEmEnT>
</ROOT>
Changing ALL nodes including the root element:
var doc = XDocument.Load("Original.xml");
// also need to change the root element
doc.Root.Name = doc.Root.Name.LocalName.ToLower();
foreach (var element in doc.Descendants().Elements())
{
element.Name = element.Name.LocalName.ToLower();
}
doc.Save("Modified.xml");
Results in Modified.xml
<?xml version="1.0" encoding="utf-8"?>
<root>
<innertag>SoMe TeXt</innertag>
<nestedelement>
<innernesting>SoMe TeXt</innernesting>
</nestedelement>
</root>
When using
foreach (var element in doc.Descendants().Elements())
{
element.Name = element.Name.LocalName.ToLower();
}
the root element will not be changed.
<?xml version="1.0" encoding="utf-8"?>
<ROOT>
<innertag>SoMe TeXt</innertag>
<nestedelement>
<innernesting>SoMe TeXt</innernesting>
</nestedelement>
</ROOT>
You can try something like this:
var doc = XDocument.Load(filepath);
foreach (var element in doc.Descendants().Elements())
{
element.Name = element.Name.LocalName.ToLower();
}

how to remove nodes in an xml that is inside another xml

I have an xml as an value of an element inside a Main xml. I want to scrub off or delete a node within the inner xml. How do I achieve that ?
For removing a node in main xml I am doing
var result = doc.Descendants("node1").Where(x => x.Attribute("id").Value == "002");
if (result != null)
{
result.Remove();
}
Here is my XML :
<?xml version="1.0" encoding="utf-16"?>
<root>
<node1>id="001" version="1.0"</node1>
<node2>id="002" version="1.0"</node1>
<report>raw = "<response = "10"><innerxml><prod>date = "18082016" name="pqr"</prod><seg1>id="002" name = "sqs"</seg1></innerxml></response>"</report>
</root>
Your code is correct but your xml is not. the XML should be like:
<?xml version="1.0" encoding="utf-16"?>
<root>
<node1 id="001" version="1.0"></node1>
<node2 id="002" version="1.0"></node2>
</root>

How to get the values of all the elements in an XML into an array?

For example :
I have the following XML,
<?xml version="1.0" encoding="utf-8"?>
<TEST>
<Name>TESTRUN</Name>
<SyncByte>ff</SyncByte>
<SOM>53</SOM>
<PDADD>7e</PDADD>
<LENLSB>08</LENLSB>
</Test>
I would like to get the values from the tags "SyncByte", "SOM", "PADD" and "LENLSB" into a single array. Is there an option within XML to accomplish this?
P.S. There are close to 20+ tags in the XML and not all tags contain values all the time. Hence if there is a single command to get all the values of the XML, then it would be great.
With Linq to Xml:
var xml = #"<?xml version=""1.0"" encoding=""utf-8""?>
<Test>
<Name>TESTRUN</Name>
<SyncByte>ff</SyncByte>
<SOM>53</SOM>
<PDADD>7e</PDADD>
<LENLSB>08</LENLSB>
</Test>";
var doc = XDocument.Parse(xml);
string[] values = doc.Root.Descendants().Select(x => x.Value).ToArray();
First:
your xml formate is wrong , it should be:
<?xml version="1.0" encoding="utf-8"?>
<TEST>
<Name>TESTRUN</Name>
<SyncByte>ff</SyncByte>
<SOM>53</SOM>
<PDADD>7e</PDADD>
<LENLSB>08</LENLSB>
</TEST> <!--END Tag should same as TEST -->
Then you can query them like this:
var xml=XDocument.Load("d:\\test.xml");
var list=xml.Descendants("TEST")
.Select(x=>new
{
SyncByte=x.Element("SyncByte")==null?"":x.Element("SyncByte").Value,
SOM=x.Element("SOM")==null?"":x.Element("SOM").Value,
LENLSB=x.Element("LENLSB")==null?"":x.Element("LENLSB").Value
});
You can get the child of any parent node through 'XmlElement.ChildNodes' and store it in collection.
for eg :
static int Main(string[] args)
{
string strFilename = "Input.xml";
XmlDocument xmlDoc = new XmlDocument();
if (File.Exists(strFilename))
{
xmlDoc.Load(strFilename);
XmlElement elm = xmlDoc.DocumentElement;
XmlNodeList lstVideos = elm.ChildNodes;
for (int i = 0; i < lstVideos.Count; i++)
Console.WriteLine("{0}",lstVideos[i].InnerText );
}
else
Console.WriteLine("The file {0} could not be located",
strFilename);
Console.WriteLine();
return 0;
}

C# modify specific node XML and delete all node with same attribute

Well I got XML like that:
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<!--Alert notes-->
<Aletrs>
<Alert>
<Name>Counter</Name>
<Value>0</Value>
</Alert>
<Alert>
<ID>1</ID>
<Name>02:46:10 - Alert ID.1 nr.1 Camera1 - photo</Name>
<Value>Capture/Images/Camera1_snapshoot_nr1-2014-03-02_02-46-10.png</Value>
</Alert>
<Alert>
<ID>1</ID>
<Name>02:46:11 - Alert ID.1 nr.2 Camera1 - photo</Name>
<Value>Capture/Images/Camera1_snapshoot_nr2-2014-03-02_02-46-11.png</Value>
</Alert>
</Alerts>
At first I need help in modify number in Value in Counter name Alert node, I got the code how to get this value but I failed to modify it into changing value and save modified XML:
string index = (from xml2 in dailyXML.Descendants("Alert")
where xml2.Element("Name").Value == "Counter"
select xml2.Element("Value").Value).FirstOrDefault();
Also I need help in code deleting all "Alert" nodes which have same value in < ID>..< /ID>
var xDoc = XDocument.Load(filename);
/*1*/
var value = xDoc.Descendants("Alert")
.FirstOrDefault(a => a.Element("Name").Value == "Counter")
.Element("Value");
value.Value = "1";
/*2*/
foreach(var alert in xDoc.Descendants("Alert")
.GroupBy(a => (string)a.Element("ID")))
{
alert.Skip(1).ToList().ForEach(x => x.Remove());
}

Using XmlTextReader to Loop though XML attributes that have the same name

I am doing some practice code with the XmlTextReader. I have written some very basic XML as shown here:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<task name="mixed_sprite_task_test">
<sprite>
<type>animatedSprite</type>
<id>0</id>
<name>animatedSprite</name>
<fileName>iyezoz</fileName>
<startingPositionX>200</startingPositionX>
<startingPositionY>200</startingPositionY>
<sheetSizeX>12</sheetSizeX>
<sheetSizeY>35</sheetSizeY>
<startingFrameX>0</startingFrameX>
<startingFrameY>0</startingFrameY>
<startingState>standing</startingState>
<movementSpeed>15</movementSpeed>
<frameDelay>0.055</frameDelay>
</sprite>
<sprite>
<type>staticSprite</type>
<id>0</id>
<name>staticSprite</name>
<fileName>Super_Mario_63</fileName>
<startingPositionX>0</startingPositionX>
<startingPositionY>0</startingPositionY>
</sprite>
<sprite>
<type>simpleSprite</type>
<id>0</id>
<name>simpleSprite</name>
<fileName>imgres</fileName>
<startingPositionX>100</startingPositionX>
<startingPositionY>100</startingPositionY>
<movementSpeed>15</movementSpeed>
</sprite>
</task>
This file shows that I have a task. In the task I have 3 sprites.
In my code I want to loop through each sprite and collect the information.
I can get the data from the first sprite with no issue. Is there a certain way to loop through an xml with attributes of the same name?
Thank-you!
I prefer Linq2Xml.
var xDoc = XDocument.Parse(xmlstring); //or XDocument.Load(filename);
var sprites = xDoc.Descendants("sprite")
.Select(s=>s.Elements()
.ToDictionary(e=>e.Name.LocalName,e=>(string)e))
.ToList();
You can use it as
var type = sprites[0]["type"];
or can take a safe action
string delay;
if (sprites[1].TryGetValue("frameDelay", out delay))
{
Console.WriteLine(delay);
}
You can select all the nodes named "sprite"
var myXml = new XmlDocument();
myXml.Load(myDocument);
XmlNode rootElement = myXml.DocumentElement;
foreach (XmlNode item in rootElement.SelectNodes(#"/task/sprite"))
{
// do stuff with node
}

Categories