I am able to parse and extract the inner text within xml tags. I am not able to extract the values present in Job tag
My Xml file format
<?xml version="1.0" standalone="yes"?>
<SmPpProperties>
<SmProperties>
<Job Name="Job001" CreatedDate="2012-10-15T10:43:56.0972966-06:00" ModifiedDate="2012-10- 15T10:46:07.6878231-06:00">
// **I am not able to extract the values present in above Job tag**
<NuclearSystem>Barium</NuclearSystem>
</Job>
</SmProperties>
<SmPpProperties>
C# code
// Load XML Document
XmlDocument MyDoc = new XmlDocument();
// Select Node
MyDoc.Load(@"C:\Users\SRangarajan\Desktop\12001_.xml");
XmlNode MyNode = MyDoc.SelectSingleNode("SmPpProperties/SmProperties/Job");
Console.WriteLine(String.Concat("NuclearSystem: ", MyNode.InnerText));
Console.ReadKey();
XmlNode.InnerText returns concatenated value of node and its child notes. That will give you Barium
. Name, ModifiedDate and CreatedDate are attributes.
Your intent is not clear, but If you want to get concatenated values of all attributes:
String.Join(",", MyNode.Attributes.Cast<XmlAttribute>().Select(a => a.Value))
Or you can get value of particular attribute by name or index:
string name = MyNode.Attributes["Name"].Value;
string createdDate = MyNode.Attributes[1].Value;
NOTE: I always suggest you use Linq to Xml for parsing xml. You can easily create strongly typed object from xml:
var xdoc = XDocument.Load(@"C:\Users\SRangarajan\Desktop\12001_.xml");
XElement j = xdoc.Root.Element("SmProperties").Element("Job");
var job = new {
Name = (string)j.Attribute("Name"),
CreatedDate = (DateTime)j.Attribute("CreatedDate"),
ModifiedDate = (DateTime)j.Attribute("ModifiedDate"),
NuclearSystem = (string)j.Element("NuclearSystem")
};
That give you job object, which will have strongly-typed properties for name, created date and modified date. And date properties will have DateTime
type!
try to use linq , like this:
string xml = "xml";
XDocument xdoc = XDocument.Parse(xml);
XElement nuclear = xdoc.Descendants(document.Root.Name.Namespace + "NuclearSystem").FirstOrDefault();
string nuclearSystem = nuclear.Value();
In order to get the Attributes, use the Attributes property and then access the Value:
XmlNode MyNode = MyDoc.SelectSingleNode("SmPpProperties/SmProperties/Job");
Console.WriteLine(String.Concat("Name: ", MyNode.Attributes["Name"].Value));
Console.WriteLine(String.Concat("CreatedDate: ", MyNode.Attributes["CreatedDate"].Value));
Console.WriteLine(String.Concat("ModifiedDate: ", MyNode.Attributes["ModifiedDate"].Value));
Console.WriteLine(String.Concat("NuclearSystem: ", MyNode.InnerText));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.