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);
}
}
}
}
}
Related
I trying to get "elements" from node and show in MessageBox
My XML: <Item Name="Test" Count="5"/>
Elements:
Name
Count
My Code:
XmlNodeList xmlNodes = xmlDocument.SelectNodes("Item");
foreach (XmlNode xmlNode in xmlNodes)
{
MessageBox.Show(xmlNode.InnerText);
}
But I do not know how to do this
You need to use Attributes. Check my solution
XmlNodeList xmlNodes = xmlDocument.SelectNodes("Item");
foreach (XmlNode xmlNode in xmlNodes)
{
foreach (XmlAttribute attr in xmlNodes.Attributes)
{
MessageBox.Show($"Attribute Name is {attr.Name} and Value is {attr.Value}");
}
}
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.
I have a file in that format
<?xml version="1.0" encoding="UTF-8"?>
<AMG>
<Include File="..."/> <!-- comment -->
<Include File="...."/> <!-- comment -->
<AMGmers Name="Auto">
<Array Type="move" Name="move_name"/>
</AMGmers>
<AMGmers Name="Black" Parent="Auto">
<Attr Type="Color" Name="auto_Params"/>
</AMGmers>
<!-- comment -->
</AMG>
I have to get all name from <AMGmers>, and I have to check availability Parent.
I was trying to do so
XmlDocument doc1 = new XmlDocument();
doc1.Load("test.xml");
XmlNodeList elemList1 = doc1.GetElementsByTagName("Name");
Please help me understand.
Since <AMG> is the root node and <AMGmers> tags are inside <AMG>, you can get all <AMGmers> tags using this syntax
XmlNodeList elemList1 = doc1.SelectNodes("AMG/AMGmers");
I assume you want to get the value of Name attribute from all <AMGmers> tags and check whether each <AMGmers> tag has Parent attribute, so this code should work
foreach (XmlNode node in elemList1)
{
if (node.Attributes["Name"] != null)
{
string name = node.Attributes["Name"].Value;
// do whatever you want with name
}
if (node.Attributes["Parent"] != null)
{
// logic when Parent attribute is present
// node.Attributes["Parent"].Value is the value of Parent attribute
}
else
{
// logic when Parent attribute isn't present
}
}
EDIT
If you want to get the <Array> nodes inside <AMGmers>, you can do so as below
foreach (XmlNode node in elemList1)
{
XmlNodeList arrayNodes = node.SelectNodes("Array");
foreach (XmlNode arrayNode in arrayNodes)
{
if (arrayNode.Attributes["Type"] != null)
{
// logic when Type attribute is present
// arrayNode.Attributes["Type"].Value is the value of Type attribute
}
}
}
EDIT 2
If you want to enumerate all nodes inside <AMGmers>, you can do so as below
foreach (XmlNode node in elemList1)
{
foreach (XmlNode childNode in node.ChildNodes)
{
// do whatever you want with childNode
}
}
Given this xml doc
<listOfItem>
<Item id="1">
<attribute1 type="foo"/>
<attribute2 type="bar"/>
<property type="x"/>
<property type="y"/>
<attribute3 type="z"/>
</Item>
<Item>
//... same child nodes
</Item>
//.... other Items
</listOfItems>
Given this xml document, I would like to select, for each "Item" node, just the "property" child nodes. How can I do it in c# directly? With "directly" I mean without selecting all the child nodes of Item and then check one by one. So far:
XmlNodeList nodes = xmldoc.GetElementsByTagName("Item");
foreach(XmlNode node in nodes)
{
doSomething()
foreach(XmlNode child in node.ChildNodes)
{
if(child.Name == "property")
{
doSomethingElse()
}
}
}
You can use SelectNodes(xpath) method instead of ChildNodes property:
foreach(XmlNode child in node.SelectNodes("property"))
{
doSomethingElse()
}
Demo.
Try using LINQ to XML instead of XML DOM as it's much simpler syntax for what you want to do.
XDocument doc = XDocument.Load(filename);
foreach (var itemElement in doc.Element("listOfItems").Elements("Item"))
{
var properties = itemElement.Elements("property").ToList();
}
How should I populate combobox with XML attribute. My XML file is:
<dataSources>
<dataSource id="1" name="support" dbtype="Oracle" dataSource="foo" initialCatalog="" userId="bar" password="x" />
</dataSources>
<services>
I need to populate 2 comboboxes with XML attribute names. I have the below code also but right now I am not getting the required output?
XmlDocument doc = new XmlDocument();
doc.Load("abc.xml");
XmlNodeList colorList = doc.SelectNodes("config/dataSources");
foreach (XmlNode dataSources in colorList)
{
comboBox1.Items.Add(dataSources.InnerXml);
}
foreach (XmlNode dataSources in colorList)
{
comboBox2.Items.Add(dataSources.InnerXml);
}
You want value of attribute name:
XmlDocument doc = new XmlDocument();
doc.Load("abc.xml");
XmlNodeList colorList = doc.SelectNodes("config/dataSources/dataSource");
foreach (XmlNode dataSources in colorList)
{
comboBox1.Items.Add(dataSources.Attributes["name"].Value.ToString());
}
Try this:
foreach (XmlNode dataSources in colorList)
{
foreach(XmlAttribute attribute in dataSources.Attributes)
{
comboBox1.Items.Add(attribute.Name); // add the attribute name to cb1
comboBox2.Items.Add(attribute.Value); // add the attribute value to cb2
}
}