create if statements blocks based on the value of an XML element - c#

How can i create if statements blocks based on the value of id (XML element)
string filepath = Server.MapPath("XMLFile2.xml");
XmlDocument xdoc1 = new XmlDocument();
xdoc1.Load(filepath);
XmlNode root = xdoc1.DocumentElement;
XmlNode idNode = root.SelectSingleNode("/students/student/id");
if (idNode.Value == 1.ToString()){my code}
im afraid that this code selects the first node in the file,,, or this there another way i can select the node based on its id value????

Replace your this code
Previous code
XmlNode idNode = root.SelectSingleNode("/students/student/id");
New code
XmlNode idNode = root.SelectSingleNode("//students/student/id");
And ya mostly if you want to search node base on the value of id than use following.
XmlNode idNode = root.SelectSingleNode("//students/student/[id='"+<YOUR id>+"']");
It will work...

You can use LINQ2XML
XElement doc =XElement.Load(filepath);
var xpath = String.Format("//students/student/[id='{0}']", "1");
-
|->your ID value goes here
var StudentNodeWithID1= doc.XPathSelectElement(xpath);
//selects a single student node with id as 1 or would return NULL if there are no students with id as 1
OR
var StudentNodeWithID1= doc.Elements("student")
.Where(s => s.Element("id").Value == "1")
.SingleOrDefault();
StudentNodeWithID1.Element("id");//id node
StudentNodeWithID1.Element("id").Value;//id value

Related

Copy Node and change Value of an Attribute

I have the following XML File. I want to copy a new "Test" and change the ID of the Test. How is it possible?
I already can copy the nodes, unfortunately not on the correct position (see images) and I also canĀ“t change the ID.
Anyone have a solution for me?
Before:
After:
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Before.xml");
XmlNode Set = xmldoc.DocumentElement;
string strXmlQuery = "/Toolings/Testing/Test1";
XmlNode NodeToCopy = Set.SelectSingleNode(strXmlQuery);
XmlNode NewNode = NodeToCopy.CloneNode(true);
NodeToCopy.AppendChild(NewNode);
Set.InsertAfter(NewNode, Set.LastChild);
XPathNavigator navigator = xmldoc.CreateNavigator();
navigator.MoveToRoot();
navigator.MoveToFirstChild();
navigator.MoveToFirstChild();
navigator.MoveToFirstChild();
navigator.MoveToFirstChild();
navigator.SetValue("5678");
xmldoc.Save(After.xml");
Here is an example using System.Xml.Linq.XDocument which is a much easier API than XmlDocument:
//You can also use Load(), this is just so I didn't have to make a file
XDocument doc = XDocument.Parse("<Toolings><Testing><Test><ID>1234</ID></Test></Testing></Toolings>");
//Grab the first Test node (change the predicate if you have other search criteria)
var elTest = doc.Descendants().First(d => d.Name == "Test");
//Copy the node, only necessary if you don't know the structure at design time
XElement el = new XElement(elTest);
el.Element("ID").Value = "5678";
//inject new node
elTest.AddAfterSelf(el);
doc.Save("After.xml");

How to get the sibling of an xml node

I have an xml file as below
<Games>
<Game>
<name>Tzoker</name>
<file>tzoker1</file>
</Game>
<Game>
<file>lotto770</file>
</Game>
<Game>
<name>Proto</name>
<file>proto220</file>
</Game>
</Games>
I want to get the values of name and file items for every Game node.
It is easy by using this query.
string query = String.Format("//Games/Game");
XmlNodeList elements1 = xml.SelectNodes(query);
foreach (XmlNode xn in elements1)
{
s1 = xn["name"].InnerText;
s2 = xn["file"].InnerText;
}
The problem is that there are some nodes that they don't have the name item. So the code above doesn't work.
I have solved the problem by using the following code
string query = String.Format("//Games/Game/name");
XmlNodeList elements1 = xml.SelectNodes(query);
foreach (XmlNode xn in elements1)
{
s1 = xn.InnerText;
string query1 = String.Format("//Games/Game[name='{0}']/file", s1);
XmlNodeList elements2 = xml.SelectNodes(query1);
foreach (XmlNode xn2 in elements2)
{
s2 = xn2.InnerText;
}
}
The problem is that there is a case that two or more nodes have the same name value. So, the s2 variable will get the file value of the last node that the loop finds. So, I would like to find a way to get the sibling file value of the current name item. How could I do it? I try do move to the parent node of the current node and then to move to the file item but without success by using the following code.
string query = String.Format("//Games/Game/name");
XmlNodeList elements1 = xml.SelectNodes(query);
foreach (XmlNode xn in elements1)
{
s1 = xn.InnerText;
string query1 = String.Format("../file");
XmlNodeList elements2 = xml.SelectNodes(query1);
foreach (XmlNode xn2 in elements2)
{
s2 = xn2.InnerText;
}
}
I hope there is a solution.
You can use Game[name] to filter Game elements to those with child element name. This is possible because child:: is the default axes which will be implied when no explicit axes mentioned. Extending this further to check for child element file as well, would be as simple as Game[name and file] :
string query = String.Format("//Games/Game[name]");
XmlNodeList elements1 = xml.SelectNodes(query);
foreach (XmlNode xn in elements1)
{
s1 = xn["name"].InnerText;
s2 = xn["file"].InnerText;
}
Now to answer your question literally, you can use following-sibling:: axes to get sibling element that follows current context element. So, given the context element is name, you can do following-sibling::file to return the sibling file element.
Your attempt which uses ../file should also work. The only problem was, that your code executes that XPath on xml, the XmlDocument, instead of executing it on current name element :
XmlNodeList elements2 = xn.SelectNodes("../file");
If I understand you correctly you want to find all games that have a name. You can do that using XPath. Here is a solution that uses LINQ to XML. I find that easier to work with than XmlDocument:
var xDocument = XDocument.Parse(xml);
var games = xDocument
.Root
.XPathSelectElements("Game[child::name]")
.Select(
gameElement => new {
Name = gameElement.Element("name").Value,
File = gameElement.Element("file").Value
}
);
The XPath to select all <Game> elements that have a <name> child element is Game[child::name].

How do i get the parent xml based on value of child xml

I have an requirement like,I retrieved id and supplier from an xml which has more than 40 ID's and Suppliers.Now all i need is want to get the parent node of particular Id and Supplier and append it to another xml.
I however managed to retrieve ID and Supplier,now i want to get the whole xml in c#.Any help would be appreciable..
c#
var action = xmlAttributeCollection["id"];
xmlActions[i] = action.Value;
var fileName = xmlAttributeCollection["supplier"];
xmlFileNames[i] = fileName.Value;
This is the code i have used to get ID and supplier.
You may want to be a little more specific about how, you are traversing the Xml Tree, and give your variables types so we can understand the problem more clearly. In saying that here is my answer:
Assuming items[i] is an XmlNode, and in this case we are working with the "hoteId" node, there is a property called XmlNode.ParentNode which returns the immediate ancestor of a node, or null if it is a root node.
XmlNode currentNode = items[i] as XmlNode; //hotelId
XmlNode parentNode = currentNode.ParentNode; //hotelDetail
string outerXml = parentNode.OuterXml; //returns a string representation of the entire parent node
Full example:
XmlDocument doc = new XmlDocument();
doc.Load("doc.xml");
XmlNode hotelIdNode = doc.SelectSingleNode("hoteldetail//hotelId"); //Find a hotelId Node
XmlNode hotelDetailNode = hotelIdNode.ParentNode; //Get the parent node
string hotelDetailXml = hotelDetailNode.OuterXml; //Get the Xml as a string
You can get Parent XML like : XmlNode node = doc.SelectSingleNode("//hoteldetail"); node.innerXml;
I think you'll be better off using linq.
var xDoc = XDocument.Parse(yourXmlString);
foreach(var xElement in xDoc.Descendants("hoteldetail"))
{
//this is your <hoteldetail>....</hoteldetail>
var hotelDetail = xElement;
var hotelId = hotelDetail.Element("hotelId");
//this is your id
var id = hotelId.Attribute("id").Value;
//this is your supplier
var supplier = hotelId.Attribute("supplier").Value;
if (id == someId && supplier == someSupplier)
return hotelDetail;
}

Reader Nodes from XML

I have this XML and i try to read the scpefic nodes but not work =(
the my code is:
XmlDocument doc = new XmlDocument();
doc.Load("https://apps.db.ripe.net/whois/search.xml?query-string=193.200.150.125&source=ripe");
XmlNode node = doc.SelectSingleNode("/whois-resources/objects/attributes/descr");
MessageBox.Show(node.InnerText);
the two circled values on image
Url: https://apps.db.ripe.net/whois/search.xml?query-string=193.200.150.125&source=ripe
it is possible?
When you are looking for a node which has an attribute "name" set to a particular value, you need to use different syntax.
You're looking for something like:
XmlNode node = doc.SelectSingleNode("/whois-resources/objects/object/attributes/attribute[#name=\"descr\"]");
XmlAttribute attrib = node.Attributes["value"];
MessageBox.Show(attrib.Value);
This will select your second node example, get the value of the value attribute, and display it.
How about using Linq To Xml?
var xDoc = XDocument.Load("https://apps.db.ripe.net/whois/search.xml?query-string=193.200.150.125&source=ripe");
var desc = xDoc.Descendants("attribute")
.Where(a => (string)a.Attribute("name") == "descr")
.Select(a => a.Attribute("value").Value)
.ToList();
or
var desc = xDoc.XPathSelectElements("//attribute[#name='descr']")
.Select(a => a.Attribute("value").Value)
.ToList();

Get XmlNodeList if a particular element value or its attribute value is present in a given list of strings

I would like to get XmlNodeList from a huge XML file.
Conditions:
I have a List of unique ID values, say IDList
Case I: Collect all the nodes where element called ID has value from IDList.
Case II: Collect all nodes where one of the attribute called idName of element ID has value from IDList.
In short, extract only the nodes which match with the values given in the IDList.
I did this using some loops like load this XML to XmlDocument to iterate over all nodes and ID value but what I am looking for is some sophisticated method to do it faster and in quick way.
Because looping isn't a solution for a large XML file.
My try:
try
{
using (XmlReader reader = XmlReader.Create(URL))
{
XmlDocument doc = new XmlDocument();
doc.Load(reader);
XmlNodeList nodeList = doc.GetElementsByTagName("idgroup");
foreach (XmlNode xn in nodeList)
{
string id = xn.Attributes["id"].Value;
string value = string.Empty;
if (IDList.Contains(id))
{
value = xn.ChildNodes[1].ChildNodes[1].InnerText; // <value>
if (!string.IsNullOrEmpty(value))
{
listValueCollection.Add(value);
}
}
}
}
}
catch
{}
XML (XLIFF) structure:
<XLIFF>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2">
<file date="2013-07-17">
<body>
<id idName="test_001" >
<desc-group name="test_001">
<desc type="text"/>
</desc-group>
<result-unit idName="test_001_text">
<source>abcd</source>
<result>xyz</result>
</result-unit>
</id>
</body>
</file>
</xliff>
Collect all the nodes like above where idName matches.
EDIT
This is a test that can parse the example you are giving. It attempts to reach the result node directly, so that it stays as efficient as possible.
[Test]
public void TestXPathExpression()
{
var idList = new List<string> { "test_001" };
var resultsList = new List<string>();
// Replace with appropriate method to open your URL.
using (var reader = new XmlTextReader(File.OpenRead("fixtures\\XLIFF_sample_01.xlf")))
{
var doc = new XmlDocument();
doc.Load(reader);
var root = doc.DocumentElement;
// This is necessary, since your example is namespaced.
var nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("x", "urn:oasis:names:tc:xliff:document:1.2");
// Go directly to the node from which you want the result to come from.
foreach (var nodes in idList
.Select(id => root.SelectNodes("//x:file/x:body/x:id[#idName='" + id + "']/x:result-unit/x:result", nsmgr))
.Where(nodes => nodes != null && nodes.Count > 0))
resultsList.AddRange(nodes.Cast<XmlNode>().Select(node => node.InnerText));
}
// Print the resulting list.
resultsList.ForEach(Console.WriteLine);
}
You can extract only those nodes you need by using an XPath query. A brief example on how you 'd go about it:
using (XmlReader reader = XmlReader.Create(URL))
{
XmlDocument doc = new XmlDocument();
doc.Load(reader);
foreach(var id in IDList) {
var nodes = doc.SelectNodes("//xliff/file/body/id[#idName='" + id + "']");
foreach(var node in nodes.Where(x => !string.IsNullOrEmpty(x.ChildNodes[1].ChildNodes[1].InnerText)))
listValueCollection.Add(node.ChildNodes[1].ChildNodes[1].InnerText);
}
}
The xpath expression is of course an example. If you want, you can post an example of your XML so I can give you something more accurate.

Categories