I have a xml file in below format
I want to fetch the value of Child1
When I am using below code it is providing a null value. Please help
XDocument xmlDoc = XDocument.Load(fileName);
XElement po = xmlDoc.Root.Element("Root");
XElement el1 = po.Element("Child1");
Use this:
xmlDoc.Descendants("Child1").First();
Related
i would like to realize this functionality:
determin, whether the XML file has a XElement with specific attribute or not.
This is the example XML code:
<root>
<pou objectId="name">
</pou>
<pou objectId="value">
</pou>
<pou objectId="address">
</pou>
</root>
I would like to determin, whether the XML file has a specific XElement "pou" with attribute ObjectId "name" or not.
The following is my code in C# using Descendants
XDocument xdoc = XDocument.Load(#"C:\Users\jsc\Desktop\TestForInherit.xml");
XDocument xNew = new XDocument();
xNew.Add(new XElement("root"));
if (xdoc.Descendants("pou").Where(x=> (string)x.Attribute("objectId") =="name").Any()==true)
{
xNew.Add(new XElement("pou", new XAttribute("objectId", "name")));
}
xNew.Save(#"C:\Users\jsc\Desktop\TestForInheritNew.xml");
If the xml file has the XElement with specific attribute value, then add thie XElement to the new XML file.
But unfortunatelly it does not work. Can anyone give me some advise.
Please try the following syntax to check if there's an attribute with specific name:
xdoc.Descendants("pou").Where(x=> x.Attribute("objectId") != null)
I have generated xmldocument by this code with C#,
protected XDocument generateXML()
{
XDocument xdoc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement("Invoices",
new XElement("Invoice",
new XElement("InvoiceNumber", "s10838652")
.......
return xdoc;
}
and in another method I have:
public override void RunWintrackConnector()
{
XDocument xml = generateXML();
.....
Then I would like to put data in each XML node: (instead of s10838652 I would like to assign (string.Concat(bill.invoice, bill.num);) to the InvoiceNumber node.)
I have the right part but not sure how get access to each node of xml:
xmlnode(for example InvoiceNumber) = Win2.IntegrationXML.XMLMisc.DirtyData.getStringValue(string.Concat(bill.invoice, bill.num));
xml
.Elements("Invoices")
.Elements("Invoice")
.Elements("InvoiceNumber")
.First()
.Value = string.Concat(bill.invoice, bill.num);
edit - to go over all your invoices:
foreach(var invoice in xml.Elements("Invoices").Elements("Invoice"))
{
invoice.Element("InvoiceNumber").Value = "asdf";
}
If you are familiar with XPath, this is equivalent to selecting all invoices with "Invoices/Invoice".
You can use XElement or XDocument to write the values you need.
var valueToInsert = Win2.IntegrationXML.XMLMisc.DirtyData
.getStringValue(string.Concat(bill.invoice, bill.num));
XDocument xml = generateXML();
xml.Element("Invoices")
.Elements("Invoice").First() //this First is just an example
.Element("InvoiceNumber")
.Value = valueToInsert;
Element() Returns the first found child node, and Elements() gets a list of all child nodes with that node name. Find out your pattern of how nodes are nested and you should be all set.
As a side note, you can do the above with a XDocument variable, but I prefer to keep everything as XElement for the added Linq compatibility.
Having problems getting NodeList.SelectSingleNode() to work properly.
My XML looks like this:
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<inm:Results xmlns:inm="http://www.namespace.com/1.0">
<inm:Recordset setCount="18254">
<inm:Record setEntry="0">
<!-- snip -->
<inm:Image>fileName.jpg</inm:Image>
</inm:Record>
</inm:Recordset>
</inm:Results>
The data is a long series of <inm:Record> entries.
I open the doc and get create a NodeList object based on "inm:Record". This works great.
XmlDocument xdoc = new XmlDocument();
xdoc.Load(openFileDialog1.FileName);
XmlNodeList xRecord = xdoc.GetElementsByTagName("inm:Record");
I start looping through the NodeList using a for loop. Before I process a given entry, I want to check and see if the <inm:Image> is set. I thought it would be super easy just to do
string strImage = xRecord[i].SelectSingleNode("inm:Image").InnerText;
My thinking being, "For the XRecord that I'm on, go find the <inm:Image> value ...But this doesn't work as I get the exception saying that I need a XmlNameSpaceManager. So, I tried to set that up but could never get the syntax right.
Can someone show me how to use the correct XmlNameSpaceManager syntax in this case.
I've worked around the issue for now by looping through all of the childNodes for a given xRecord, and checking the tag once I loop around to it. I would like to check that value first to see if I need to loop over that <inm:Record> entry at all.
No need to loop through all the Record elements, just use XPath to specify the subset that you want:
XmlDocument xdoc = new XmlDocument();
xdoc.Load(openFileDialog1.FileName);
XmlNamespaceManager manager = new XmlNamespaceManager(xdoc.NameTable);
manager.AddNamespace("inm", "http://www.inmagic.com/webpublisher/query");
XmlNodeList nodes = xdoc.SelectNodes("/inm:Results/inm:Recordset/inm:Record[inm:Image != '']", manager);
Using the LINQ to XML libraries, here's an example for retrieving that said node's value:
XDocument doc = XDocument.Load(openFileDialog1.FileName);
List<XElement> docElements = doc.Elements().ToList();
XElement results = docElements.Elements().Where(
ele => ele.Name.LocalName == "Results").First();
XElement firstRecord = results.Elements().Where(
ele => ele.Name.LocalName == "Record").First();
XElement recordImage = firstRecord .Elements().Where(
ele => ele.Name.LocalName == "Image").First();
string imageName = recordImage.Value;
Also, by the way, using Hungarian notation for a type-checked language is overkill. You don't need to prepend string variables with str when it will always be a string.
XmlNamespaceManager nsMgr = new XmlNamespaceManager(xdoc.NameTable);
string strImage = xRecord[i].SelectSingleNode("inm:Image",nsMgr).InnerText;
Should do it.
Using this Xml library, you can get all the records that have an Image child element with this:
XElement root = XElement.Load(openFileDialog1.FileName);
XElement[] records = root.XPath("//Record[Image]").ToArray();
If you want to be sure that the Image child contains a value, it can be expressed like this:
XElement[] records = root.XPath("//Record[Image != '']").ToArray();
Q:
How to get the attribute value of the root element(first element in my xml file) through LINQ.
.cs :
XDocument xmlDoc = XDocument.Load(targetFileName);
.xml :
<timetable ascttversion="2010" options="idprefix:realID">
I want to read the options value.
Something like this:
XDocument xdoc = XDocument.Load(targetFileName);
var attrib = xdoc.Root.Attribute("options").Value;
// attrib = "idprefix:realID"
Following should do
xmlDoc.Root.Attribute("option").Value
Using old way of doing via XmlDocument,
string xmlstring = "<root><profile><Name>John</Name><Age>23</Age></profile></root>";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlstring);
string childNodes = xmlDoc.SelectSingleNode("root/Profile").InnerXml;
// output of childNodes would be => "<Name>John</Name><Age>23</Age>";
what is the equivalent of doing the above execution in LinQ when you have XElement variable. I see XPathSelectElement method in XElement but it doesn't return the child nodes + Child nodes text. Any Ideas?
I wouldn't use XPath at all for this. I'd use:
XDocument doc = XDocument.Parse(xmlString);
var nodes = doc.Root
.Elements("profile")
.DescendantsAndSelf();
That give the profile nodes and all their descendants. It's not really clear what you're trying to do with the results, but if you can give more details I should be able to come up with the appropriate code.