AppendChild in a for-loop (node transfer) - c#

I am a beginner in c#, so don't expect a lot from me...
How can I transfer nodes from one xml file to another and store it in the new xml file using c#?
The procedure I took was the following:
Load 2 xml files by using variablename.load(filepath)
Creating XmlElements which store root node of the imported xml-files.
Nested for-loop across the nodes in the xml files.
If nodes of the documents are the same, don't do anything
Else: Remove the content of the previous XML file and update it with the content of the new XML-file
Apparently implementing appendChild results in a System.NullReferenceException: error

Related

How to pass different values to XML node before passing to HttpWebRequest

I am new to C# and got a question. I am writing automation test for SOAP web service using HttpWebRequest and I am passing XML to request and getting XML back as string.
Now, I am trying to pass this request XML saved in file but wants to change XML node values. I am thinking of having different test data from an Excel sheet and then passing that data to XML nodes.
I can do this if I saved all XML request in a string object within a class but I want to read request from XML file.
I am not sure how to do that.
There are different ways to parameterize XML and send to a webservice. Easiest way is to create a template XML with placeholders and then at run-time replace the placeholders with data from excel, as shown below.
XML:
<Name>
<FirstName>{{FirstName}}</FirstName>
<LastName>{{LastName}}</LastName>
</Name>
Excel/CSV
FirstName,LastName
FName1,LName1
FName2,LName2
You can now read the values from the excel as a dictionary and do something like below.
C# Code Illustration:
foreach(KeyValuePair kvp in dictionary)
{
xml = xml.Replace("{{" + kvp.key + "}}", kvp.value)
}
If the XML parameterization is not feasible, you can navigate to the XML nodes using XPath (Use any of the XML holder objects) and set the node values.

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?

Map multiple xml feeds to one object

Using C# I want to be able to map a number of xml feeds to one custom object. Each xml feed has the same kind of data but has its own naming convention.
Ideally i would like to store for each xml feed its own mapping and apply that automatically when copying the xml data to my object. I would like to do this as the system may grow to hundreds of feeds so just being able to store the mappings would make it easier to maintain than writing code for each feed.
So for example, my object consists of
ID, Name
And xml feed one is
Code, ProductName
xml feed two is
UniqueID, FullName
so the mappings would be
ID -> Code
Name -> ProductName
and
ID -> UniqueID
Name -> FullName
What would be the best way of achieving this?
I would create a configsection in your config file. You could then have a node for each feed. Then have nodes within that have the mapping information. The nodes in your feed node would match the properties in your c# object and the node value would be the node name in your xml file. You could also even add the full xpath path if it was more complicated.
<feed url="">
<id>Code</id>
<Name>ProductName</Name>
</feed>
Then in your app you could load the feed. Then search for the node in your config file to get how to map the fields to your C# object from fields in your xml file.
Just one approach that would make it easy to configure and grow without changing the application unless your c# object changes.

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.

How can I add new root element to a C# XmlDocument?

I have, outside of my control, an XmlDocument which has a structure like the following:
<parent1>
...minor amount of data...
</parent1>
I have another XmlDocument, also outside of my control, which has the following structure:
<parent2>
..very large amount of data...
</parent2>
I need an XmlDocument in the format:
<parent1>
...minor amount of data...
<parent2>
..very large amount of data...
</parent2>
</parent1>
I don't want to make a copy of parent2. How can I get the structure I need, without copying parent2? I believe this means
oParent1.DocumentElement.AppendChild(oParent1.ImportNode(oParent2.DocumentElement, true));
is out of the question.
Any good solutions out there?
Just remove the DocumentElement from the parent2 XmlDocument, then append the imported parent1 node to the XmlDocument (directly -- NOT to the DocumentElement) and re-append the removed parent2 node to the imported parent1 node:
var p1node = oParent2.ImportNode(oParent1.DocumentElement, true);
var p2node = oParent2.RemoveChild(oParent2.DocumentElement);
oParent2.AppendChild(p1node);
p1node.AppendChild(p2node);

Categories