Retrieving an Attribute Value from an XNode - c#

I have my XML file loaded and then I select my last element. Here is the code:
XDocument doc = XDocument.Load("something.xml");
var last = doc.Root.LastNode;
The code above outputs the last element on the XML file. Here is the code:
<link num="4" url="yahoo.com">Yahoo</link>
I want to be able to select the value 4 of num. Here is the code:
num="4"
How can I select the number 4 from my last node?

Try this:
XDocument xDoc = XDocument.Parse(xml);
string num = xDoc.Root.Elements().Last().Attribute("num").Value;
Console.WriteLine(num);
Make sure you have added the following using:
using System.Linq;
using System.Xml.Linq;

You can get value with following code.
var numValue = ((XElement)last).Attributes("num").FirstOrDefault().Value

Related

Adding a root node to Xml Document having a existing root node in C#

I have an xml document that may look like this
<Stuff>
<SomeStuff></SomeStuff>
</Stuff>
and I want to add a new root to this document, to make it look like this
<Root>
<Stuff>
<SomeStuff></SomeStuff>
</Stuff>
</Root>
This is what i tried
string inputXml = " <Stuff>
<SomeStuff></SomeStuff>
</Stuff>";
XmlDocument firstLossRootNode = new XmlDocument();
firstLossRootNode.LoadXml("<Root />");
var economyDocument = = XDocument.Parse(inputXml);
firstLossRootNode.DocumentElement.AppendChild(economyDocument.Document);
I was trying to Append it has a child but i was getting compile time error
Is there a way to do this in c# using Xdocument class.
In your example, you are using the XmlDocument class for your firstLossRootNode, but the XDocument class for your economyDocument. Is this by design? If not, the following code will do what you are trying to do:
using System;
using System.Xml.Linq;
string inputXml = "<Stuff><SomeStuff></SomeStuff></Stuff>";
XDocument firstLossRootNode = XDocument.Parse("<Root />");
XDocument economyDocument = XDocument.Parse(inputXml);
firstLossRootNode.Root.Add(economyDocument.FirstNode);

Get a xml element with specific attribute value in c#

I need to get a value of a SubTopic element which has an attribute called "Name" with specific value. I do it this way;
IEnumerable<XElement> list =
(from el in xdoc.Elements()
where (string)el.Attribute("Name") == "creatingTests"
select el);
The collection has zero elements.
I tried putting xdoc.Elements("SubTopic") instead of empty parameter, but with no success.
My XML file structure;
<?xml version="1.0" encoding="windows-1250" ?>
<Help Title="TestTool - tematy pomocy">
<Topic Name="creatingTests" Title="Tworzenie testów">
<SubTopic Name="saveload" Title="Zapis i odczyt z pliku">
Content
</SubTopic>
</Topic>
</Help>
How can I get that value of Help/Topic(Name="creatingTests")?
xdoc is of course XDocument object with loaded xml and it does have the content of my file.
xdoc.Elements() returns only one element - the Root of XML tree (it's <Help> element in your example.
Change your query to:
IEnumerable<XElement> list =
(from el in xdoc.Root.Elements()
where (string)el.Attribute("Name") == "creatingTests"
select el);
It returns collection with one element. Use First or FirstOrDefault to get it as single item, not a collection:
XElement item = (from el in xdoc.Root.Elements()
where (string)el.Attribute("Name") == "creatingTests"
select el).FirstOrDefault();
Here's an alternative by using System.Xml.XPath:
using System.Xml.Linq;
using System.Xml.XPath;
class Program
{
static void Main(string[] args)
{
var xdoc = XDocument.Load("input.xml");
var subTopic = xdoc
.XPathSelectElement("//Topic[#Name='creatingTests']/SubTopic");
}
}
Very easy and simplest way is to use XSLT..
1.Create an XSLT Template.
2.Call it in c#.
xmlDaynamic.DocumentContent = "Your XML Input";
xmlDaynamic.TransformSource = "YourTemplate with extension";
3.Your task is done.
4.xmlDaynamic is a server control.
Try using XPATH
http://support.microsoft.com/kb/308333
"//Topic[#Name='creatingTests']"

How do I insert inner text into empty xml element?

I have an xmldocument that i'm loading xml in to.
The xml looks like this:
<Table1>
<buyer_id>0</buyer_id>
<buyername>CompanyA</buyername>
<address1>123 Simpsons Dr.</address1>
<address2/>
<city>Springfield</city>
<state>ST</state>
<postalcode>12345</postalcode>
<eaddress/>
<phone/>
<fax/>
</Table1>
I'm looping through looking at each CompanyA entry and setting innertext accordingly. I'm using the following code to insert inner text into elements that meet the criteria:
XmlDocument dom = new XmlDocument();
dom.LoadXml(xmlString);
XmlNodeList elemList = dom.GetElementByTagName("Table1");
for(int i = 0; i < elemList.Count; i++)
{
if(dom.GetElementsByTagName("buyername").Item(i).InnerText.Contains("CompanyA")
{
dom.GetElementsByTagName("address1").Item(i).InnerText = "SomeInfo";
}
}
Using the above code, the value of address1(123 Simpsons Dr.) would be replaced by "SomeInfo". I would like to instead insert "SomeInfo" into the address2 element but when I try using:
dom.GetElementsByTagName("address2").Item(i).InnerText = "SomeInfo";
I get an error. I'm able to insert innertext into any element that already has a value but I cannot when the element is empty (such as <address2/>). Thoughts?
Use LINQ2XML.It's a complete replacement to other XML api's like the dirty old idiot XmlDocument
XElement doc=XElement.Load("yourXml.xml");
foreach(var elm in doc.Descendants("Table1"))
{
if(elm.Element("buyername").Value=="CompanyA")
elm.Element("address2").Value="SomeInfo";
}
doc.Save("yourXml.xml");
Check if the address2 xml tag is empty.
If yes , go to its parent and remove the tag then again add the same tag with value.
If no , assign the inner text to address2.
let me know if you need the code.
Use the SetElementValue method in LINQ to XML:
XDocument doc = XDocument.Load(FilePath); //replace with xml file path
IEnumerable<XElement> buyersList = doc.Descendants("Table1"); //get the table node.
var ele = (from buyer in buyersList
where buyer.Element("buyername").Value == "CompanyA"
select buyer).SingleOrDefault();
ele.SetElementValue("address1", "SomeInfo");
ele.SetElementValue("address2", "SomeInfo");
doc.Save(FilePath);
DEMO: http://ideone.com/Cf7YI

Get xml node using c#

I have a request that returns a large xml file. I have the file in a XmlDocument type in my application. From that Doc how can I read an element like this:
<gphoto:videostatus>final</gphoto:videostatus>
I would like to pull that value final from that element. Also If i have multiple elements as well, can I pull that into a list? thanks for any advice.
If you already have an XmlDocument then you can use the function GetElementsByTagName() to create an XmlNodeList that can be accessed similar to an array.
http://msdn.microsoft.com/en-us/library/dc0c9ekk.aspx
//Create the XmlDocument.
XmlDocument doc = new XmlDocument();
doc.Load("books.xml");
//Display all the book titles.
XmlNodeList elemList = doc.GetElementsByTagName("title");
for (int i=0; i < elemList.Count; i++)
{
Console.WriteLine(elemList[i].InnerXml);
}
You can select nodes using XPath and SelectSingleNode SelectNodes. Look at http://www.codeproject.com/Articles/9494/Manipulate-XML-data-with-XPath-and-XmlDocument-C for examples. Then you can use for example InnerText to get final. Maybe you need to work with namespaces (gphoto). The //videostatus would select all videostatus elements
You can try using LINQ
XNamespace ns = XNamespace.Get(""); //use the xmnls namespace here
XElement element = XElement.Load(""); // xml file path
var result = element.Descendants(ns + "videostatus")
.Select(o =>o.Value).ToList();
foreach(var values in value)
{
}
Thanks
Deepu

How do I get XML path by its value?

I need to get the xml path opf the xml file by providing the value of an xml child element as the input.
For example:
XML file:
<?xml version="1.0"?>
<document-inquiry xmlns="http://ops.epo.org">
<publication-reference data-format="docdb" xmlns="http://www.epo.org/exchange">
<document-id>
<country>EP</country>
<doc-number>1000</doc-number>
<kind>A1</kind>
</document-id>
</publication-reference>
</document-inquiry>
For the above XML file. I need to get the XML path by using the value "1000".
If my input is value of the element "1000"
Output i need is :
<document-id>
<country>EP</country>
<doc-number>1000</doc-number>
<kind>A1</kind>
</document-id>
I need to achieve this using c# code. Can anyone please help me out on this...
You could use XPathSelectElement extension method:
using System;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
class Program
{
static void Main()
{
var doc = XDocument.Load("test.xml");
var ns = new XmlNamespaceManager(new NameTable());
ns.AddNamespace("ns", "http://www.epo.org/exchange");
var elem = XDocument.Load("test.xml")
.XPathSelectElement("//ns:document-id[ns:doc-number='1000']", ns);
if (elem != null)
{
Console.WriteLine(elem.ToString());
}
}
}
You could use XPathSelectElements if you want to select multiple nodes that correspond to this criteria.
You can select the element that you want with a linq query.
var number = "1000";
var xml = XDocument.Parse( xml_string );
XNamespace ns = "http://www.epo.org/exchange";
var result = (from data in xml.Descendants(ns + "document-id")
where data.Element(ns + "doc-number").Value == number
select data).FirstOrDefault();
.FirstOrDefault() returns the first matching element or null. You could instead use .List() to get a list containing all matching elements.

Categories