How to get the node value by passing type in XDocument C#
Here is solution with Xml Linq (XDOCUMENT) :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication107
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
var results = doc.Descendants("subscription_add_on").Select(x => new
{
add_on_code = (string)x.Element("add_on_code"),
name = (string)x.Element("name"),
quantity = (int)x.Element("quantity"),
amount = (int)x.Element("unit_amount_in_cents"),
add_on_type = (string)x.Element("add_on_type")
}).ToList();
}
}
}
C# Xdocument Xml get Element
Here's an alterante way:
var doc = XDocument.Load(xml);
XNamespace ns1 = "http://schemas.xmlsoap.org/soap/envelope/";
XNamespace ns2 = "http://www.borger.dk/2009/WSArticleExport/v1";
XNamespace ns3 = "http://www.borger.dk/2009/WSArticleExport/v1/types";
var result = doc.Element(ns1 + "Envelope")
.Element(ns1 + "Body")
.Element(ns2 + "GetAllArticlesResponse")
.Element(ns2 + "GetAllArticlesResult")
.Elements(ns3 + "ArticleDescription")
.Select(x => x.Value);
Or
var doc = XDocument.Load(xml);
var envelope = doc.Root;
var body = (XElement)envelope.FirstNode;
var getAllArticlesResponse = (XElement)body.FirstNode;
var getAllArticlesResult = (XElement)getAllArticlesResponse.FirstNode;
var articleDescriptions = getAllArticlesResult.Nodes().Cast<XElement>();
var result = articleDescriptions.Select(x => x.Value);
Finding Specific xml nodes using XDocument
Try following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XNamespace ns = doc.Root.GetNamespaceOfPrefix("tns");
var results = doc.Descendants(ns + "responses")
.Where(x => x.Elements(ns + "Include").Any())
.Select(x => new {
include = (string)x.Element(ns + "Include"),
name = (string)x.Element(ns + "Name")
}).ToList();
}
}
}
Parsing Nested Nodes from XML in XDocument
Use linq to xml.
var xml = XElement.Load(xmlUrl);
int id = (int)xml.Element("Animal").Attribute("id");
var active = xml.Element("Animal")
.Element("Information")
.Element("AvailableTypes")
.Element("AvailableType")
.Element("Active");
var animalName = active.Element("AnimalName").Value;
var animalPictureNode = active.Element("AnimalPictures").Element("AnimalPicture");
var animalPicture = animalPictureNode.Value;
var animalPictureType = animalPictureNode.Attribute("type").Value;
Console.WriteLine(id);
Console.WriteLine(animalName);
Console.WriteLine(animalPicture);
Console.WriteLine(animalPictureType);
Open namespace:
using System.Xml.Linq;
XDocument get and set values in XML nodes
Your first attempt is almost right. There are a couple things missing:
- The namespace defined in code must be an exact match to the one in the xml. In your case the namespace in code has an extra trailing slash. It should be
http://schemas.xmlsoap.org/ws/2002/12/secext
. - The XPath expression should be
//wss:Security/wss:UsernameToken/wss:Username
. The leading slashes basically mean "look for this node anywhere". Alternatively, you could write out the whole path begining with<S:Envelope>
. You would need to add the soap envelope namespace to your code as well.
C# XDocument Read all Nodes from XML-File
You need to use the Value
property, i.e. sb.Append(xe.Value)
.
How to get value of child node from XDocument
If there's only one Cust
element and only one ACTNumber
element, then it's easy:
string actNumber = doc.Root.Element("Cust").Element("ACTNumber").Value;
Or to get it as a long
:
long actNumber = (long) doc.Root.Element("Cust").Element("ACTNumber");
How to get value of an XML node?
When using an XDocument
you have to go via its Root
property.
String xml = @"
<ns0:Visit xmlns:ns0=""http://Co.Burgers.Ues"">
<ns0:SourcePartyName>NDHARY</ns0:SourcePartyName>
</ns0:Visit>
";
XDocument xdoc = XDocument.Parse(xml);
XNamespace ns = "http://Co.Burgers.Ues";
String sourcePartyName = (String)xdoc.Root.Element(ns + "SourcePartyName");
Related Topics
How to Set Web.Config File to Show Full Error Message
How to Remove an Xml Element from File
Set Database Timeout in Entity Framework
Could Not Find Installable Isam
Limit the Number of Parallel Threads in C#
Custom JSON Serialization for Each Item in Ienumerable
Getmanifestresourcestream Returns Null
How to Get a Count of the Total Number of Digits in a Number
How to Load All Assemblies from Within Your /Bin Directory
JSONconvert.Serializeobject Always Return {} in Xamarinforms
What's the Point of a Lambda Expression
Validating for Large Files Upon Upload
How to Clear Event Subscriptions in C#
How to Convert a Character in to Equivalent System.Windows.Input.Key Enum Value
How to Get the Correct Ip from Http_X_Forwarded_For If It Contains Multiple Ip Addresses