How to modify XML file in c#? - c#

<Customers>
<Customer1>
<Name>Bobby</Name>
<Age>21</Age>
<Address>Panjim</Address>
</Customer1>
<Customer2>
<Name>Peter</Name>
<Age>32</Age>
<Address>Panjim</Address>
</Customer2>
<Customer4>
<Name>Joel</Name>
<Age>32</Age>
<Address>Mapusa</Address>
</Customer4>
</Customers>
So the thing is I want to delete a particular element and when i delete the first element i.e customer1, I want to update the other elements. I mean I want to make customer3, customer2 and customer2, customer1.
Can anyone please help me achieve this?

What about:
class Program {
static void Main(string[ ] args) {
XDocument doc = XDocument.Load("D:\\file.xml"); //example file
doc.Root.SwitchAndRemove("Customer1");
doc.Save("D:\\file.xml");
}
}
public static class Utilities {
public static void SwitchAndRemove(this XElement customers, XName name) {
var x = customers.Descendants().Where(e => e.Name == name).Select((element, index) => new { element, index }).Single();
int count = 0;
XElement temp = x.element;
foreach (XElement el in customers.Nodes()) {
if (count == x.index + 1) {
temp.RemoveAll();
temp.Add(el.Descendants().ToArray());
temp = el;
}
else
count++;
}
temp.Remove();
}
}
By giving as input your xml the output is the following:
<?xml version="1.0" encoding="utf-8"?>
<Customers>
<Customer1>
<Name>Peter</Name>
<Age>32</Age>
<Address>Panjim</Address>
</Customer1>
<Customer2>
<Name>Joel</Name>
<Age>32</Age>
<Address>Mapusa</Address>
</Customer2>
</Customers>

I'd argue that your problem is not how you could rename your nodes with minimum effort but structure of your XML file.
You said order of customers is not important and apparently customer tag's number is not important, either, since you want to rename the tags upon deletion.
So maybe this structure just creates unnecessary complexity and extra work for you.
Only reason I see you could need the number in tag is to identify the node you are about to remove. Am I right or is there something more to it? If not then you could add random unique identifier (like Guid) to your customer data to remove the right one.
Could save you lot of trouble.
<customers>
<customer>
<guid>07fb-877c-...</guid>
<name>Notch</name>
<age>34</age>
<address>street</address>
</customer>
<customer>
<guid>1435-435a-...</guid>
<name>Sam</name>
<age>23</age>
<address>other</address>
</customer>
<customers>

Say the element you have to delete is Customer1, first of all you can read the complete xml file using one of the XML parsing classes available in c# like XDocument or XmlReader and write to another xml file say "Temp.xml" skipping the Customer1 element completely. This way we have achieved the deletion part.
Next to update, forget the file being XML file and read the entire file to a string, say "xmlstring". Now use the Replace function available with a string data type to replace "Customer2" with "Customer1" and then "Customer3" with "Customer2" and so on.
And now delete your original XML file and write the string "xmlstring" using a stream writer to a file name "YourFileName.xml"
Thats it. Hope this solution works for you. Try this and in case u are unable get this done, share the code which u tried and we shall suggest how to work it out.

taken from your comment that the order does not have to be preserved then you can do this
public static void RemoveCustomer(XElement customers, XElement removeThis){
var last = customeers.Elements().Last();
if(last != removeThis){
foreach(var element in removeThis.Elements()){
element.Value = last.Element(element.Name).Value;
}
}
last.Remove();
}
It effectively substitutes the one to be removed with the last (unless the last should be removed) and thereby eliminates the need for renaming any of the other elements

Related

How to check if a value already exists in an xml file using linq

Although there are many answers to this topic, I couldn't find one that worked in my case.
The app opens the xml file to add new entries from a list, but prevents duplicates. I don't know how to check (using Linq) if the item is already in the xml
<!-- XML File sample -->
<?xml version="1.0" encoding="UTF-8"?>
<Items>
<Item>
<Text>AUDI</Text>
</Item>
<Item>
<Text>BMW</Text>
</Item>
</Items>
and this is the code. (I left out trim, uppercase, etc for simplicity)
The problem is in the result var, it always return false.
XDocument doc = XDocument.Load(#filePath);
for (int i = 0; i < items.Count; i++)
{
var result = doc.Descendants("Item").Any(x => x.Element("Text").ToString().Equals(items[i]);
if (! result)
{
}
doc.Save(#filePath);
Your problem is :
x.Element("Text").ToString()
To get the string inside the Text node use .Value
Like so:
XDocument doc = XDocument.Load(#filePath);
for (int i = 0; i < items.Count; i++)
{
var result = doc.Descendants("Item").Any(x => x.Element("Text").Value.Equals(items[i]));
if (!result)
{
}
}
doc.Save(#filePath);
I believe you're misinterpreting the format of your XML.. You're looking to match the InnerXml of the Text element which you never do here. So you need to move from Root -> Items -> Item -> Text -> Value which isn't exactly what's happening with your code.
So at a minimum you need to use the Value reference on the Text element (x.Element("Text").Value). Also, I think your call to Descendants directly returns the Text elements so I would recommend inspecting the IEnum after that and confirming what level of the xml you're at at that point. I haven't used that method but my understanding is that it gives you descendants of the xpath you provide which means it gives you Text elements. If that is the case change that string to "Items" and you'll be good to go.

Xml Linq Find Element After another Element

I am searching for this for a long time here, but I can't get it working from other codes. I need to find the closest element to "user" (which is "robot") and write its value (it's depending on user's input). I am programming a chat-bot. This is my XML file:
<Answers>
<user>This is a message</user><robot>Here is an answer</robot>
<user>This is another message</user><robot>Here is another answer</robot>
</Answers>
In C# code i am trying something like this:
private static void Main(string[] args)
{
XDocument doc = XDocument.Load("C:\\bot.xml");
var userPms = doc.Descendants("user");
var robotPm = doc.Descendants("robot");
string userInput = Console.ReadLine();
foreach (var pm in userPms.Where(pm => pm.Value == userInput))
{
Console.WriteLine // robotPm.FindNextTo(pm)
}
}
Simply put, I want to compare "user" input in console and in xml and if they are equal write robot's answer from xml which is responsible to specified user input.
Thank you for help
Simply use NextNode
Console.WriteLine(((XElement)pm.NextNode).Value);
But don't forget: Altough I've never seen a counter-example, xml parsers do not guarantee the order of elements. a better approach would be
<item>
<q>qusetion1</q>
<a>answer1</a>
</item>
<item>
<q>qusetion2</q>
<a>answer2</a>
</item>

How to delete certain root from xml file?

My '.xml' file looks this way:
<?xml version="1.0" encoding="utf-8"?>
<Requestes>
<Single_Request num="1">
<numRequest>1</numRequest>
<IDWork>1</IDWork>
<NumObject>1</NumObject>
<lvlPriority>Высокий</lvlPriority>
</Single_Request>
<Single_Request num="2">
<numRequest>2</numRequest>
<IDWork>2</IDWork>
<NumObject>2</NumObject>
<lvlPriority>Средний</lvlPriority>
</Single_Request>
<Periodic_Request num="1">
<numRequest>3</numRequest>
<IDWork>23</IDWork>
<pFrequency>23</pFrequency>
<lvlPriority>Низкий</lvlPriority>
<time_service>23</time_service>
<time_last_service>23</time_last_service>
<relative_time>23</relative_time>
</Periodic_Request>
</Requestes>
So I need to delete Single_Request with atribute value equal to sTxtBlock_numRequest.Text. I have tried to do it this way:
XDocument doc = XDocument.Load(FilePath);
IEnumerable<XElement> sRequest = doc.Root.Descendants("Single_Request").Where(
t => t.Attribute("num").Value =="sTxtBlock_numRequest.Text"); //I'm sure, that problem is here
sRequest.Remove();
doc.Save(FilePath);
Unfortunattly, nothing has happanned, don`t know how to solve the problem.
This is why , I am looking forward to your help.
You are comparing attribute value with string literal "sTxtBlock_numRequest.Text". You should pass value of textbox text instead:
doc.Root.Elements("Single_Request")
.Where(t => (string)t.Attribute("num") == sTxtBlock_numRequest.Text)
.Remove();
Note - it's better to use Elements when you are getting Single_Request elements of root, because Descendants will search whole tree, instead of looking at direct children only. Also you can call Remove() without saving query to local variable.

XML comparer C#

I want to compare 2 XML files.
Looks easy if both of them have an identical structure. But not in my case :(
My files looks like:
<root>
<t>
<child1>
<cc1>val</cc1>
<cc2>val</cc2>
......
</child1>
<child2>
<cc1>val</cc1>
<cc2>val</cc2>
......
</child2>
<child2>
<cc1>val</cc1>
<cc2>val</cc2>
......
</child2>
.......
<child3>
<cc1>val</cc1>
<cc2>val</cc2>
......
</child3>
....
</t>
<t>
...
</t>
.....
</root>
And they could have any numbers of childes, and childes of childes...
The task is
To compare only one defined block. I need search it for value of 1st child's child (child1.cc1.value in this example)
During the comparetion some nodes could be skipped (the names of skipped nodes stored somewhere, for example, in strings array)
It is possible to have multiple identical nodes like . And if child2 isn't ignored, then I need to make sure they are the same amount, and they all coincide with the corresponding second file. So there could be next situation:
1st file contains:
<child2><cc1>1</cc1>...</child2>
<child2><cc1>3</cc1>...</child2>
<child2><cc1>2</cc1>...</child2>
2st file contains:
<child2><cc1>2</cc1>...</child2>
<child2><cc1>1</cc1>...</child2>
<child2><cc1>3</cc1>...</child2>
And that means they are corresponds each other.
So they could be in the random order.
Now I can't make a decision how to realize this algorithm. I suggested to use DataSet objects, but this XML-structure looks too difficult for simply using DataTables, dataRows and etc..
Now I'm trying XmlNodes. But I haven't realized that part where I have several identical nodes with different data in random order.
Any ideas?
How large are your XML files? And how complex is the structure in reality?
If not too large or complex then I would recommend parsing the whole file into a class structure and then performing your validation on the properties of the classes. For example (pseudocode)...
xmlClass file1 = new xmlClass(file1info);
xmlClass file2 = new xmlClass(file2info);
//Custom classes have now parsed XML files in whichever way you like
if (file1.numberOfChildren != file2.numberOfChildren)
{
//comparison fail
}
elseif (!file1.orderOfChildrenSame(file2))
{
//comparison fail
}
else
{
//comparison success
}
Obviously the exact implementation of the methods and properties of your xmlClass will depend on your exact requirements.
XmlClass may be of the rough layout...
using System;
using System.Collections.Generic;
using System.Xml;
public class XmlClass
{
private XmlDocument _xmlDoc;
private List<ChildClass> _children As New List<ChildClass>();
public XmlClass(FileInfo fil){
_xmlDoc = New XmlDocument();
_xmlDoc.Load(fil.FullName);
ParseChildren();
_xmlDoc = Nothing;
}
private void ParseChildren(){
XmlNodeList ndl = _xmlDoc.SelectNodes("/root/t") //select all <t>s
foreach (xmlNode nodT in ndl.Nodes){
foreach (xmlNode nodChild in nodT.ChildNodes()){
_children.Add(new ChildClass(nodChild));
}
}
// Now _children contains all child nodes of <t>s and can be worked with logically
}
public int numberOfChildren
{
get {return _children.Count();}
}
}
You will obviously need to implement ChildClass - which may in turn contain a collection of ChildClass itself (allowing the hierarchy you describe). You will also need to implement the other validation methods as you require. Also you may need to implement other classes to represent other node types within the document which you are interested in.
Don't parse more than you need to in order to validate! - It depends what your end goal is.
PS
I would also suggest that this XML format is not very "nice" in terms of the <child1>, <child2> set-up. It would be much more XMLesque to have <child id="1">, <child id="2"> etc. As presumably <child1> and <child2> are essentially the same type of node...

How to save XML node back into XML file with LINQ-to-XML?

I've got an XML file which I use to create objects, change the objects, then save the objects back into the XML file.
What do I have to change in the following code so that it extracts a node from the XML based on the id, replaces that node with the new one, and saves it back into the XML?
The following gives me 'System.Xml.Linq.XElement' does not contain a constructor that takes '0' arguments':
//GET ALL SMARTFORMS AS XML
XDocument xmlDoc = null;
try
{
xmlDoc = XDocument.Load(FullXmlDataStorePathAndFileName);
}
catch (Exception ex)
{
HandleXmlFileNotFound(ex);
}
//EXTRACT THE NODE THAT NEEDS TO BE REPLACED
XElement oldElementToOverwrite = xmlDoc.Descendants("smartForm")
.Where(sf => (int)sf.Element("id") == 2)
.Select(sf => new XElement());
//CREATE THE NODE THAT WILL REPLACE IT
XElement newElementToSave = new XElement("smartForm",
new XElement("id", this.Id),
new XElement("idCode", this.IdCode),
new XElement("title", this.Title)
);
//OVERWRITE OLD WITH NEW
oldElementToOverwrite.ReplaceWith(newElementToSave);
//SAVE XML BACK TO FILE
xmlDoc.Save(FullXmlDataStorePathAndFileName);
XML file:
<?xml version="1.0" encoding="utf-8" ?>
<root>
<smartForm>
<id>1</id>
<whenCreated>2008-12-31</whenCreated>
<itemOwner>system</itemOwner>
<publishStatus>published</publishStatus>
<correctionOfId>0</correctionOfId>
<idCode>customerSpecial</idCode>
<title>Edit Customer Special</title>
<description>This form has a special setup.</description>
<labelWidth>200</labelWidth>
</smartForm>
<smartForm>
<id>2</id>
<whenCreated>2008-12-31</whenCreated>
<itemOwner>system</itemOwner>
<publishStatus>published</publishStatus>
<correctionOfId>0</correctionOfId>
<idCode>customersMain</idCode>
<title>Edit Customer</title>
<description>This form allows you to edit a customer.</description>
<labelWidth>100</labelWidth>
</smartForm>
<smartForm>
<id>3</id>
<whenCreated>2008-12-31</whenCreated>
<itemOwner>system</itemOwner>
<publishStatus>published</publishStatus>
<correctionOfId>0</correctionOfId>
<idCode>customersNameOnly</idCode>
<title>Edit Customer Name</title>
<description>This form allows you to edit a customer's name only.</description>
<labelWidth>100</labelWidth>
</smartForm>
</root>
Well, the error has nothing to do with saving, or even with replacement - it has to do with you trying to create an XElement without specifying the name. Why are you trying to use Select at all? My guess is you just want to use Single:
XElement oldElementToOverwrite = xmlDoc.Descendants("smartForm")
.Where(sf => (int)sf.Element("id") == 2)
.Single();
(As Noldorin notes, you can give Single a predicate to avoid using Where at all. Personally I quite like to split the two operations up, but they'll be semantically equivalent.)
That will return the single element in the sequence, or throw an exception if there are 0 elements or more than one. Alternatives are to use SingleOrDefault, First, or FirstOrDefault:
SingleOrDefault if it's legal to have 0 or 1
First if it's legal to have 1 or more
FirstOrDefault if it's legal to have 0 or more
If you're using an "OrDefault" one, the result will be null if there are no matches.
I think the problem is simply your use of the Select call in the statement assigning oldElementToOverwrite. You actually seem to want the Single extension method.
XElement oldElementToOverwrite = xmlDoc.Descendants("smartForm")
.Single(sf => (int)sf.Element("id") == 2)

Categories