How to insert the new element on XMLNode in C# - c#

I am trying to insert a new element into the XmlNode in C# code,
How to insert the <delimiter>##<delimiter> element in inside the "/TestBooks/template/field" root. (Screenshot1)
enter image description here
Inside the <field> element, i need to insert the <delimiter> element, based on id element <Id>11-09-2020-505</Id>. (Screenshot2)
enter image description here

First of all you really shouldn't use pictures in questions -- we are taking the time to type you an answer you can take the time to use copy and paste and format your questions.
I'd like to answer your questions but I'm concerned about the questions because it implies that you are adding in markers to aid in the parsing of the data. You really shouldn't need to parse XML.
There are many great parsers of XML including one built into C# you should not roll your own.
In the XML standard the order of the children not defined. If you put a child as the "first" child, there is no reason to expect that a parser would list it as first.
(To have order in children you should just add an order attribute)
Because of these reason it should not matter where in the list of children you add the child.
So we can tell you how to add a child but we can't "put it in a specific spot" since children don't have an order.
In summary, it is not possible to do what you ask.

Related

Nested XML to CSV, generic code

This is a tough one.
I have to deal with some XML files we've got from our suppliers. The files contain the products and the variants (color, size, and such). Some of the suppliers send us plain XMLs, containing one root node per variant, having the product name repeated on every row, other suppliers send us nested XMLs, containing the product as root, variants as children nodes.
I have searched the site before asking this question, all the answers I found start with the assumption the XML structure is known.
In my case, it's not. Tomorrow I can get files from a new supplier, with a different structure/different column names.
My goal is to convert the nested xml into a plain xml (i.e. having only root nodes, and repeat the product name for all the new created rows).
Is this even possible? Any idea?

Cloning elements and changing its order by comparing xml's

I have a source xml which needs to be compared against xml template(another xml file).
The following needs to be implemented,
The order of the source xml needs to be transformed as per the order in the template xml.
If the elements in the template xml are not found in the source xml then it should be added.
Basically what should I need to do this, compare 2 xml's and need to change one xml based on other.
I have an idea of creating XDocument with 2 xml's and by accessing xpath of source xml from template xml, Rearrange the elements and add elements if not found.
Please advise whether this is a better approach, or anything can be added into this?

Finding specific tree node in TreeView

I'm adding tree nodes to TreeView programmatically based on values from DB.
I need to add child nodes to those nodes before. The only way to locate them is by myReportsNode.ChildNodes[int index], but this information is useless for me, as I need to find them by their value, or some unique id.
FindNode is not a good option, as I don't know exactly where they are, and looking for each node via itarating through the tree is a waste. I though of a dictionary for nodes.
Any ideas?
You need to use a tree traversal. If there is some sort of ordering, you could make sure they are loaded into the tree reflecting that ordering, and you can use a depth-first search and have logarithmic complexity (every bit as efficient as a tree-based Dictionary). A dictionary of nodes would be a waste of memory, since you already have a tree structure you can use--that is, if there is some sort of ordering.
If you would give us some sort of idea of how the data is in the tree, I could help even more.
Use strategy of using three four columns in your database table.
1) nodeId
2) nodeText
3) nodeValue
4) parentId
When a node is clicked, send request via ajax to find if this id has child, if yes bring its childs and append below it. Else no child found, no need to append.

Copy an existing XML element into the same document

I am building up an XML document using c#. I use AppendChild to add an element called say "test" to a parent element in the document.
I then build up more of the xmlDocument but further down the same document I wish to append the same element "test" to a different node.
I have tried using AppendChild but it added it to the new node and removed it from the existing one. Which I guess is expected. I was just wondering is there anything available that I can use to copy the existing element and add it to a new node without removing it from the existing position?
To perhaps help explain the following code results in the test element only appearing in parentElement2.
parentElement1.AppendChild(test)
...
parentElement2.AppendChild(test)
Is there anyway I can insert test into both parent elements?
Hope this makes sense.
This should be possible with the CloneNode method. It lets you create a (possibly deep) copy of a node, which you can then insert wherever you like in your document.

Handling duplicate nodes in XML

Scenario:
I am parsing values from an XML file using C# and have the following method:
private static string GetXMLNodeValue(XmlNode basenode, string strNodePath)
{
if (basenode.SelectSingleNode(strNodePath) != null)
return (basenode.SelectSingleNode(strNodePath).InnerText);
else
return String.Empty;
}
To get a particular value from the XML file, I generally pass the root node and a path like "parentnode/item"
I recently ran into an issue where two nodes at the same document level share the same name.
Why:
The duplicate nodes all need to be read and utilized; they have child nodes with differing values (which can distinguish them). At present I just have two nodes, each named <Process> and sub-nodes named <Name> which contain unique names. Other sub-nodes contain various values such as memory usage. When processing and storing the values for the sub-nodes, I would essentially ignore the parent node name and use my own names based on the sub-node <Name> value.
Question:
What is the best way to get the values for duplicate-named nodes distinctly? My thought was to load all values matching the node name into an array and then using the array index to refer to them. I'm not sure how to implement that, though. (I'm not well-versed in XML navigation.)
Sample XML
<?xml version="1.0" ?>
<info>
<processes>
<process>
<name>program1</name>
<memory_use>24.8</memory_use>
</process>
<process>
<name>program2</name>
<memory_use>19.0</memory_use>
</process>
</processes>
</info>
Use SelectNodes method instead it gives you a list of all nodes matching your Xpath
The answer to your question is, "it depends".
It depends on what you intend to do with the "duplicate" nodes. What does it mean for there to be more than one with the same name? Was it an error in the program that generated the XML? Was it correct, and an unlimited number of such nodes is permitted? What do they mean when there are more than one?
You need to answer those questions first, before designing code that processes "duplicate" nodes.

Categories