Pretty new at C# so trying to learn xml serialization. I have an xml setup like the following:
<Guy>
<Name>
<Root>
<Entry>
<Favorite> his favorite food is sushi </Favorite>
</Entry>
</Root>
</Name>
</Guy>
I need to select the "Favorite" tags and return the "his favorite food is sushi". What is the simplest way to go about this in C#? Can XDocument and LINQ extensions be used?
string xml = #"
<Guy>
<Name>
<Root>
<Entry>
<Favorite> his favorite food is sushi </Favorite>
</Entry>
</Root>
</Name>
</Guy>";
var doc = XDocument.Parse(xml);
Console.WriteLine(doc.Descendants("Favorite").First().Value);
// or
foreach(var item in doc.Descendants("Favorite").Select(e => e.Value))
{
Console.WriteLine(item);
}
https://dotnetfiddle.net/Iagop5
You can try this approach using XPathNavigator:
public static string GetAttribute(string xml, string nodeName) {
StringReader stringReader = new StringReader(xml);
XPathDocument doc = new XPathDocument(stringReader);
XPathNavigator xNav = doc.CreateNavigator();
XPathNavigator node = xNav.SelectSingleNode("//" + nodeName);
return node != null ? node.InnerXml : string.Empty;
}
Here's a reference: https://learn.microsoft.com/en-us/dotnet/api/system.xml.xpath.xpathnavigator.selectsinglenode?view=net-6.0
Related
Let say I have a XML with this format:
<TEST>
<DRINK>
<NAME>Ice tea</NAME>
<NAME>Milo</NAME>
<NAME>Coffee</NAME>
</DRINK>
<FOOD>
<NAME>Fried Rice</NAME>
<NAME>Hamburger</NAME>
<NAME>Fried Noodles</NAME>
</FOOD>
</TEST>
How to retrieve only food names and put them in the ASP.NET web form textbox?
This is my current code:
XmlDocument doc = new XmlDocument();
doc.Load(filepath);
root = doc.DocumentElement;
TextBox1.Text = root.GetElementsByTagName("NAME")[0].InnerText;
TextBox2.Text = root.GetElementsByTagName("NAME")[1].InnerText;
TextBox3.Text = root.GetElementsByTagName("NAME")[2].InnerText;
This code will instead retrieve drink names instead of food names. How to make it read NAME tags in FOOD tag?
By using XmlNode.SelectNodes Method with providing the XPath.
var foodElements = root.SelectNodes("FOOD/NAME");
Console.WriteLine(foodElement[0].InnerText);
Console.WriteLine(foodElement[1].InnerText);
Console.WriteLine(foodElement[2].InnerText);
Sample .NET Fiddle
Basic xml parsing.
this:
{
string sXML
=
#"<TEST>
<DRINK>
<NAME>Ice tea</NAME >
<NAME>Milo</NAME >
<NAME>Coffee</NAME>
</DRINK>
<FOOD>
<NAME>Fried Rice</NAME>
<NAME>Hamburger</NAME>
<NAME>Fried Noodles</NAME>
</FOOD>
</TEST>";
XmlDocument myXml = new XmlDocument();
myXml.LoadXml(sXML);
XmlNodeList myNodes = myXml.SelectNodes("TEST/FOOD/NAME");
foreach (XmlNode OneNode in myNodes)
{
Debug.Print(OneNode.InnerText);
}
}
output:
Please help. How to read from xml sub tree. I have xml doc:
<data>
<Infos>
<Info>
<AddressFk>1</AddressFk>
<AddressLine1>1970</AddressLine1>
<AddressLine2>Napa Ct.</AddressLine2>
<Phone>
<dataAsString1>111111</string>
<dataAsString2>222222</string>
<dataAsString3>333333</string>
</Phone>
<City>Bothell</City>
</Info>
</Infos>
I read xml using XDocument:
XDocument xdoc = XDocument.Load("1.xml");
foreach (XElement addresList in xdoc.Document.Element("data").Elements("Infos").Elements("Info"))
{
address = new Address();
address.id = (string)addresList.Element("AddressFk");
address.Line1 = (string)addresList.Element("AddressLine1");
address.Line2 = (string)addresList.Element("AddressLine2");
address.City = (string)addresList.Element("City");
}
how to get the structure <Phone> ???
Use Elements
var phones = addresList.Element("Phone").Elements("string");
foreach(var phone in phones)
{
Console.WriteLine((string)phone);
}
for the future, it is bad practice to use tag names with reserved words
From the following xml:
<response>
<content>
<Result xmlns="http://www.test.com/nav/webservices/types">
<Name>Test</Name>
</Result>
</content>
<status>ok</status>
</response>
I am trying to get the value of the Name element the following way but that does not work:
private static void Main()
{
var response = new XmlDocument();
response.Load("Response.xml");
var namespaceManager = new XmlNamespaceManager(response.NameTable);
namespaceManager.AddNamespace("ns", "http://www.test.com/nav/webservices/types");
Console.WriteLine(response.SelectSingleNode("/response/content/Result/Name", namespaceManager).InnerXml);
}
How can I select the Name element?
Your code would have worked just fineif the Xml had defined the namespace with a "ns:" prefix.
But in this case, the namespace is given without any prefix, which sets the default namespace for everything in the Result tag to ".../webservice/types".
To reflect this, you need to modify the Xpath, and tell the XmlDocument that the nodes you are looking for under Resultare in the webservice/types namespace. So your query will look like this:
Console.WriteLine(response.SelectSingleNode(#"/response/content/ns:Result/ns:Name", namespaceManager).InnerXml);
For getting directly the text value of a node there is a text() function, if used in the query it would look like:
/response/content/Result/Name/text()
Try this:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.InnerXml = "<response><content><Result xmlns=\"http://www.test.com/nav/webservices/types\"><Name>Test</Name></Result></content><status>ok</status>";
string elementValue = String.Empty;
if (xmlDoc != null)
{
xNode = xmlDoc.SelectSingleNode("/Result");
xNodeList = xNode.ChildNodes;
foreach (XmlNode node in xNodeList)
{
elementValue = node.InnerText;
}
}
I'm trying to parse the following:
<?xml version="1.0" encoding="utf-8"?>
<GC>
<CREATED>01/23/2014 16:10:18</CREATED>
<DATA>
<CONTAINER name="home" type="xml" version="1.1.0.0">
<HEADER>
<ATTRIBUTE name="lang" value="EN" />
<ATTRIBUTE name="destination" value="UK" />
</HEADER>
</CONTAINER>
</DATA>
</GC>
How do I go about finding the value when name="lang"?
So far I have this:
XmlDocument Doc = new XmlDocument();
Doc.Load(#path);
XmlNode node = Doc.DocumentElement.SelectSingleNode("/GC/DATA/CONTAINER/HEADER/ATTRIBUTE/NAME");
string SI = node.Attributes["lang"].InnerText;
Doesn't seem to work unfortunately, could use some help. Many thanks.
This will do it:
XmlNode node =
Doc.SelectSingleNode("/GC/DATA/CONTAINER/HEADER/ATTRIBUTE[#name = 'lang']/#value");
string SI = node.InnerText;
And I would advise using a null check:
XmlNode node =
Doc.SelectSingleNode("/GC/DATA/CONTAINER/HEADER/ATTRIBUTE[#name = 'lang']/#value");
string SI = null;
if(node != null)
{
SI = node.InnerText;
}
With using LINQ to XML you can get it like this:
XDocument xDoc = XDocument.Load("path");
var element = xDoc.Descendans("ATTRIBUTE").First();
var nameAttribute = (string)element.Attribute("name");
This will get you the value of the attribute in the ATTRIBUTE tag which has name == lang:
XmlDocument Doc = new XmlDocument();
Doc.Load(#path);
XmlNode node = Doc.DocumentElement.SelectSingleNode("/GC/DATA/CONTAINER/HEADER/ATTRIBUTE[#name='lang']");
string SI = node.Attributes["value"].InnerText;
XmlNode node = Doc.DocumentElement.SelectSingleNode("/GC/DATA/CONTAINER/HEADER/ATTRIBUTE[#name='lang']");
string SI = node.Attributes["value"].Value;
I'm navigating XML document with XPathNodeIterator, and I want to change some nodes' values.
I can't figure out how :(
Here's the code I'm using:
XPathDocument docNav = new XPathDocument(path);
XPathNavigator nav = docNav.CreateNavigator();
nav.MoveToRoot();
XPathNodeIterator itemsIterator = nav.Select("/foo/bar/item");
while (mediumsIterator.MoveNext())
{
XPathNodeIterator subitemsIterator = itemsIterator.Current.Select("SubitemsList/name");
while (subitemsIterator.MoveNext())
{
XPathNodeIterator nodesIterator = itemsIterator.Current.Select("Param");
nodesIterator.MoveNext();
String the_params = nodesIterator.Current.Value;
// check if I need to modify nodesIterator.Current.Value
// ...
// ok I do - how?
}
}
And the XML file sample:
<?xml version="1.0" encoding="utf-8"?>
<foo>
<bar>
<item>
<Param />
<SubitemsList>
<name>name one</name>
<name>name two</name>
...
</SubitemsList>
</item>
...
</bar>
</foo>
Or maybe there's a better way to do this?
I found a way:
Replace XPathDocument with XmlDocument
When I get to the needed node
...
XmlNode node = ((IHasXmlNode)nodesIterator.Current).GetNode();
node.InnerText = "new text";