How do you cycle through nodes in XML file [duplicate] - c#

This question already has answers here:
Reading Xml with XmlReader in C#
(7 answers)
Closed 6 years ago.
I have an XML file and the structure is roughly like this:
<ScenarioList>
<Scenario>
... various things
</Scenario>
</ScenarioList>
This is being read into a WPF dialog box. Every time the user selects 'Next' I want to read the next Scenario data into the various fields. Obviously, the same thing goes for the user clicking on the 'Last' button, too.
But the question is: how do i just read in the information from the selected Scenario node?
Let me to try and clarify the question since it appears that it is 'overly broad':
I am familiar with loading and reading entire XML files. I just want to selectively read specific nodes. For example: How can I read just the data for the first Scenario node? Then, depending on user input, read just the data for the second Scenario node? Then, depending on user input, read just the data for the first scenario node? Or just the third scenario node?
In essence, I'm asking since XML doesn't have an 'index' how do I specify which instance of a node to read?

First you need to load the xml into an XmlDocument, then you can select the specific nodes and iterate over them, like this:
XmlDocument xml = new XmlDocument();
xml.LoadXml("your xml");
XmlNodeList list = xml.SelectNodes("/ScenarioList/Scenario");
foreach (XmlNode xn in list)...

You can traverse to node by using JXPath.
If you need to evaluate multiple paths relative to a certain node in the object graph, you might want to create a relative JXPathContext.
First, obtain the pointer for the location that is supposed to be the root the relative context. Then obtain the relative context by calling
JXPathContext context = JXPathContext.newContext(bean);
Pointer addressPtr = context.getPointer("/employees[1]/addresses[2]");
JXPathContext relativeContext =
context.getRelativeContext(addressPtr);
// Evaluate relative path
String zipCode = (String)relativeContext.getValue("zipCode");
// Evaluate absolute path
String name = (String)relativeContext.getValue("/employees[1]/name");
// Use the parent axis to locate the employee for the current address
Double salary = (Double)relativeContext.getValue("../salary");
you can refer in more detail from here

Related

How to insert the new element on XMLNode in 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.

How do I set a xml Node to contain a string's content?

Let's say I have one string called data.
How do i put a string's text inside one of it's nodes? How do i choose which one contains the string?
unlike the other questions i saw, my xml will contain many tags. so i dont want so set it to contain one thing, but to add it to the others.
You can use the following to add a text node to your XML file with the data you have:
// Open the XML
XmlDocument doc = new XmlDocument();
doc.LoadXml("<somedata><moredata>sometext</moredata></somedata>");
// Create the new node, set to text and insert the data
XmlNode newElem = doc.CreateNode("text", "yournodename", "");
newElem.InnerText = data;
// Write the new node and append
XmlElement root = doc.DocumentElement;
root.AppendChild(newElem);
If you need something more specific, please provide some more information regarding the node name or the XML Document etc.
UPDATE: As mentioned in the comments, the above code will modify but not save the modifications to the XML. To do so, load the XML document using doc.Load("pathtoyourxml.xml") instead of using doc.LoadXml() and save it to the same path using doc.Save("pathtoyourxml.xml") after doing whatever you are doing with it.

Overwrite specific XML node

I have a XML file of the following format:
<Alarms>
<Alarm>
<Id>1</Id>
<Severity>Warning</Severity>
<Comments></Comments>
</Alarm>
<Alarm>
<Id>2</Id>
<Severity>Error</Severity>
<Comments>Restart the machine</Comments>
</Alarm>
...
My program has a GUI which gives the user the ability to edit the Comments of an alarm. I am trying to come up with the best solution for the actions to take when a user is done editing and wants to save the changes. The XML file isn't extremely large (it does not warrant a database) but large enough that I do not want to overwrite the entire thing every time a change is made to a single alarm. Is it possible to target only a specific node and edit the Comments attribute without then having to re-write everything?
I'm looking for a XML-specific solution... I want to avoid regular flat-file methods that involve going to a specific line in a file and then editing that line. Perhaps something exists for XML files that I'm not privy to. I'm currently working with a .NET 2 project but will soon be upgrading to 4.5, so any solution works for me.
You can load up the xml in XmlDocument class. Navigate with an XPath query to the Comments node you want to edit and change the value. When you are done, just save the document to the same file name or a different one.
Here is an example using a Console Application.
// The Id of the Alarm to edit
int idToEdit = 2;
// The new comment for the Alarm
string newCommentValue = "Here is a new comment";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlNode commentsElement = doc.SelectSingleNode(String.Format("Alarms/Alarm[Id = '{0}']/Comments", idToEdit));
commentsElement.InnerText = newCommentValue;
doc.Save(Console.Out);
Here is a working fiddle: https://dotnetfiddle.net/eQROet

How to dynamically change attribute of an xml node with c#

I have a c# application that saves user's data to an xml document. I want to be able to dynamically change the attribute of an xml node based on a user entering different criteria into a text box and choosing to save/overwrite the existing file save. The problem is that I can't simply delete the node and recreate it with the new attribute as the node has child nodes that can't be deleted.
Does anyone have any ideas or suggestions?
the XmlNode.Attributes method does not provide a way as I can tell to delte just the attribute of a node and reassign it. I could be wrong though.
Cast your node to an XmlElement and use the element.SetAttribute(...); method.
((XmlElement)node).SetAttribute("name", "value");
Also I believe there is a way to do it without the cast if you know the attribute already exists:
node.Attributes["name"].Value = "value";

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