I have the following xml file structure:
<configuration>
<Points>
<Point1>
<Url>net.tcp://10.1.1.144</Url>
<Domain>10.1.1.144</Domain>
<Secure>true</Secure>
<UserName>flofy</UserName>
<Password>Jojo</Password>
</Point1>
<Point2>
<Url>net.tcp://10.1.1.22</Url>
<Domain>10.1.1.22</Domain>
<Secure>false</Secure>
<UserName></UserName>
<Password></Password>
</Point2>
</Points>
</configuration>
I want to iterate over all the ponts, I tried:
var doc = new XmlDocument();
doc.Load(#"C:\myXml.xml");
var nodes = doc.DocumentElement.SelectNodes("/configuration/Points");
foreach (XmlNode n in nodes)
{
MessageBox.Show(n.Name);
}
But it prints only Points, but I want it to print Point1, Point2 etc..
You want to do..
foreach (XmlNode n in doc.SelectSingleNode("/configuration/Points").ChildNodes)
{
MessageBox.Show(n.Name);
}
Your xpath query does only select the node "Points", but you want to iterate its child nodes.
Related
Hi All: Could you please tell me what is the mistake in gettting the child & child2 attribute values?
I'm getting the valu of the node attribute but not for the childs nodes.
Overall i need to get Name value from products, Visibile value from the element Visibilities, and Price value from elment Prices.
Code:
XmlDocument doc = new XmlDocument();
doc.Load("Prices.txt");
XmlNodeList nodes = doc.GetElementsByTagName("RetroPrintProduct");
foreach (XmlNode node in nodes)
{
Console.WriteLine("Name={0} ", node.Attributes["Name"].Value);
foreach (XmlNode child in node.SelectNodes("ProductVisibility "))
{
Console.WriteLine("Visible={0}", child.Attributes["Visible"].Value);
foreach (XmlNode child2 in child.SelectNodes("Prices"))
{
Console.WriteLine("Price={0}", child2.Attributes["Price"].Value);
}
}
}
XML file:
<Item>
<RetroPrintProduct Nino123="d89e280b-c8d5-4da2-87da-36e4a57e2867" Nino1="2022ProfileProducts.RetroPrintProduct" Sys_Index="UpdateOnly" DefaultIconName="RetroPrints_10x8x2_R_Metallic.png" DefaultOutputProfileName="Nino" DefaultOutputProfileTypeFullName="2022Profile" Name="Item Retro">
<ShippingMethodPrices Index="Replace" />
<Visibilities Index="Replace" />
</RetroPrintProduct>
<RetroPrintProduct Nino123="67d1577d-7baf-4b3f-a9fb-9d52404e45f4" Nino1="2022ProfileProducts.RetroPrintProduct" Sys_Index="UpdateOnly" DefaultIconName="RetroPrints_10x8x2_S_Normal.png" DefaultOutputProfileName="Nino" DefaultOutputProfileTypeFullName="2022Profile" Name="Item 2 Retro">
<ShippingMethodPrices Index="Replace" />
<Visibilities Index="Replace">
<ProductVisibility Nino123="cc0096e0-d964-4e45-93f7-9258ddee148d" Sys_GlobalUniqueId="cc0096e0-d964-4e45-93f7-9258ddee148d" Sys_ReplicationId="39f43856-a16f-4555-b519-ccf71b97ee58" Nino1="2022.ProductVisibility" Activated="True" Noprices="False" PhotoSource="EndUserPhotos" BackgroundColor="Default" Icon="" Image="" IsUnusableByLicense="False" MaxDate="2999-12-31" MinDate="1900-01-01" Name="" Object="Visible" Visible="True" ProductLibrary="" ReplicationId="39f43856-a16f-4555-b519-ccf71b97ee58" SysCode="">
<Prices Index="Replace">
<ProductPrice Nino123="4ca2658e-3e07-4636-b33f-d87fb021288a" Sys_GlobalUniqueId="4ca2658e-3e07-4636-b33f-d87fb021288a" Sys_ReplicationId="3e75b8f4-d1b6-41fe-be9e-d2858caf6eb9" Nino1="2022.ProductPrice" FixFee="0" ServiceFee="0" Mode="Replace" FromQuantity="1" Price="0.5" ProductPriceType="PerPageQuantity" ProductLibrary="" ReplicationId="3e75b8f4-d1b6-41fe-be9e-d2858caf6eb9" SysCode="" />
</Prices>
</ProductVisibility>
</Visibilities>
</RetroPrintProduct>
</Item>
It looks like you're missing some nesting when searching through the nodes. ProductVisibility is under Visibilites, not RetroPrintProduct, and Price is an attribute of ProductPrice, not Prices.
Something like this should do the trick (note that I named the nodes in code to match the xml names to make it easier to remember which child is which):
XmlDocument doc = new XmlDocument();
doc.Load(#"c:\temp\Prices.xml");
XmlNodeList retroPrintProducts = doc.GetElementsByTagName("RetroPrintProduct");
foreach (XmlNode retroPrintProduct in retroPrintProducts)
{
Console.WriteLine("Name={0} ", retroPrintProduct.Attributes["Name"].Value);
foreach (XmlNode visibility in retroPrintProduct.SelectNodes("Visibilities"))
{
foreach (XmlNode productVisibilty in visibility.SelectNodes("ProductVisibility"))
{
Console.WriteLine("Visible={0}", productVisibilty.Attributes["Visible"].Value);
foreach (XmlNode price in productVisibilty.SelectNodes("Prices"))
{
foreach (XmlNode productPrice in price.SelectNodes("ProductPrice"))
{
Console.WriteLine("Price={0}", productPrice.Attributes["Price"].Value);
}
}
}
}
}
I have this xml file:
<table head="Film">
<row>
<id>USD</id><jan>Jan</jan><feb>Feb</feb><mar>Mar</mar><apr>Apr</apr><maj>May</maj><jun>Jun</jun><jul>Jul</jul><aug>Aug</aug><sep>Sep</sep><okt>Oct</okt><nov>Nov</nov><dec>Dec</dec><sum>Year</sum>
</row>
<row>
<id>2018</id><jan>7629</jan><feb>6433</feb><mar>5573</mar><apr>3676</apr><maj>2545</maj><jun>2542</jun><jul>266</jul><aug>276</aug><sep>2690</sep><okt>371</okt><nov>5446</nov><dec>754</dec><sum>52731</sum>
</row>
I'm trying to extract the individual values for every month.
I've tried
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("model.xml");
XmlNodeList nodeList = xmlDoc.GetElementsByTagName("table");
foreach (XmlNode node in nodeList) // for each <testcase> node
{
Console.WriteLine(node["row"].InnerText);
}
This gives an exception because node["row"] is empty.
Any ideas?
Firstly your XML is not valid. You need to have a </table> on there.
//In this example GetXml() just returns your XML
var doc = XDocument.Parse(GetXml());
var rows = doc.Descendants("table").Elements("row").ToList();
foreach(var element in rows[1].Elements()){
Console.WriteLine(element?.Value);
}
Now this is just a basic example based of your XML. You would likely want it to be more robust. You will notice I am showing you this with LINQ, I feel it's more readable than XmlDocument.
You need to loop through the child nodes to get your desired result as follow:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("model.xml");
XmlNodeList nodeList = xmlDoc.GetElementsByTagName("table");
foreach (XmlNode node in nodeList) // for each <testcase> node
{
foreach (XmlNode row in node.ChildNodes)
{
foreach (XmlNode mon in row.ChildNodes)
{
}
}
}
My XML looks like this:
<data>
<location ....>
<stores>
<store ....></store>
<store ... ></store>
</stores>
</location>
</data>
I want to loop through all the location elements, and then loop through all the stores.
I also want access to the attributes of both location and store elements.
I have loaded the xml document:
var doc = new XmlDocument();
doc.LoadXml(myXmlAsString);
Let's say this is your sample XML
<data>
<location sample="something">
<stores>
<store atrb="1">store1</store>
<store atrb="2">store2</store>
</stores>
</location>
<location>
<stores>....</stores>
</location>
</data>
Then using XDocument of System.Xml.Linq, you access all the locations, stores and their attributes like this
var xmlString = File.ReadAllText(#"C:\YourDirectory\YourFile.xml");
XDocument geneva = XDocument.Parse(xmlString);
var locations = geneva.Descendants("location");
foreach (var location in locations)
{
var sampleAttribute = location.Attribute("sample").Value;
var stores = location.Descendants("store");
foreach (var store in stores)
{
var value = store.Value;
var atrbValue = store.Attribute("atrb").Value;
}
}
If you dont want to use XmlSerializer to get the typed data you can get nodes by criteria with SelectSingleNode or SelectNodes methods. This will give you ability to iterate through location elemets.
I would suggest creating classes to deserialize to because its easier to work with.
Loop through locations:
XmlNodeList xnList = doc.SelectNodes("/data/location");
foreach(XmlNode node in xnList)
{
//your code goes here..
}
Loop through all stores:
XmlNodeList xnList = doc.SelectNodes("/data/location/stores");
foreach(XmlNode node in xnList)
{
//your code here..
}
I have a XML file which contains about 850 XML nodes. Like this:
<NameValueItem>
<Text>Test</Text>
<Code>Test</Code>
</NameValueItem>
........ 849 more
And I want to add a new Childnode inside each and every Node. So I end up like this:
<NameValueItem>
<Text>Test</Text>
<Code>Test</Code>
<Description>TestDescription</Description>
</NameValueItem>
........ 849 more
I've tried the following:
XmlDocument doc = new XmlDocument();
doc.Load(xmlPath);
XmlNodeList nodes = doc.GetElementsByTagName("NameValueItem");
Which gives me all of the nodes, but from here am stuck(guess I need to iterate over all of the nodes and append to each and every) Any examples?
You need something along the lines of this example below. On each of your nodes, you need to create a new element to add to it. I assume you will be getting different values for the InnerText property, but I just used your example.
foreach (var rootNode in nodes)
{
XmlElement element = doc.CreateElement("Description");
element.InnerText = "TestDescription";
root.AppendChild(element);
}
You should just be able to use a foreach loop over your XmlNodeList and insert the node into each XmlNode:
foreach(XmlNode node in nodes)
{
node.AppendChild(new XmlNode()
{
Name = "Description",
Value = [value to insert]
});
}
This can also be done with XDocument using LINQ to XML as such:
XDocument doc = XDocument.Load(xmlDoc);
var updated = doc.Elements("NameValueItem").Select(n => n.Add(new XElement() { Name = "Description", Value = [newvalue]}));
doc.ReplaceWith(updated);
If you don't want to parse XML using proper classes (i.e. XDocument), you can use Regex to find a place to insert your tag and insert it:
string s = #"<NameValueItem>
<Text>Test</Text>
<Code>Test</Code>
</NameValueItem>";
string newTag = "<Description>TestDescription</Description>";
string result = Regex.Replace(s, #"(?<=</Code>)", Environment.NewLine + newTag);
but the best solution is Linq2XML (it's much better, than simple XmlDocument, that is deprecated at now).
string s = #"<root>
<NameValueItem>
<Text>Test</Text>
<Code>Test</Code>
</NameValueItem>
<NameValueItem>
<Text>Test2</Text>
<Code>Test2</Code>
</NameValueItem>
</root>";
var doc = XDocument.Load(new StringReader(s));
var elms = doc.Descendants("NameValueItem");
foreach (var element in elms)
{
element.Add(new XElement("Description", "TestDescription"));
}
var text = new StringWriter();
doc.Save(text);
Console.WriteLine(text);
I have an xml that looks like this
<words>
<word>word1</word>
<word>word2</word>
<word>word3</word>
<word>word4</word>
</words>
I would like to loop through the "word" tags and just output the innertext for now.
how would I do this?
here is what i am doing now but is says the list of nodes count is 1
string _badWordFileDocPath = //my file path;
XmlDocument badWordDoc = new XmlDocument();
badWordDoc.Load(_badWordFileDocPath);
XmlElement root = badWordDoc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("/words");
foreach(XmlNode node in nodes)
{
Console.WriteLine(node.InnerText);
}
Thanks!
You are selecting the root words node itself, rather than the child word nodes. Add /word to your XPath:
XmlNodeList nodes = root.SelectNodes("/words/word");
You need to move down the node tree one more layer:
foreach(XmlNode node in nodes)
{
XmlNodeList innerNodes = node.SelectNodes("/word");
foreach(Xmlnode innerNode in innerNodes )
{
Console.WriteLine(innerNode.InnerText);
}
}
I'd recommend using the classes in System.Xml.Linq for this task:
XElement wordsElement = XElement.Parse(yourXmlText);
var words = from w in wordsElement.Elements("word")
select w.Value;
You are missing /word in your XPath.
You can also use Linq (XDocument) to fetch the data.