Parse data from XML - c#

I am struggling to read out the "ProtectedElements" node of the xml file below.
I can read out the information above that node without an issue, but I cant seem to find a way to read out the checksum from that particular node.
When i try to read it, the code returns "null".
I have tried to look up the documentation from Microsoft but the examples dont seem to help. I guess it is something to do with the nesting but I cant figure out a solution.
I only want to read out the value of the attribute.
Any help appreciated.
var doc = XDocument.Load(TempFile);
// get LockInfo element
var lockInfoElement = doc.Root.Element(ns + "LockInfo");
// get lockDate
var lockDateAttr = lockInfoElement.Attribute("lockDate");
var lockDateText = lockDateAttr.Value;
var lockDate = DateTime.Parse(lockDateText);
// get locked by
var lockedByAttr = lockInfoElement.Attribute("lockedBy");
var lockedByText = lockedByAttr.Value;
var lockedBy = lockedByText.ToString();
// get ValidationInfo element
var validationInfoElement = doc.Root.Element(ns + "ValidationInfo");
var validationDateAttr = validationInfoElement.Attribute("validationDate");
var validationDateText = validationDateAttr.Value;
var validationDate = DateTime.Parse(validationDateText);
// get ValidationInfo element
var validationByAttr = validationInfoElement.Attribute("validatedBy");
var validationByText = validationByAttr.Value;
var validationBy = validationByText.ToString();
// get ConfigSeal checksum element
var configInfoElement = doc.Root.Element(ns + "ConfigurationSeal");
var configurationSealAttr = configInfoElement.Attribute("checksum");
var configurationSealChkSum = configurationSealAttr.Value;
var configurationSealText = configurationSealChkSum.ToString();
//--------------------------------------------------------------------
// get Protected elements checksum
var protectedElements = doc.Root.Element(ns + "ProtectedElements");
var protectedElementsAttr = protectedElements.Attribute("checksum");
var protectedElementsChkSum = protectedElementsAttr.Value;
var protectedElementsText = protectedElementsChkSum.ToString();
XML DATA:
<SafetyConfiguration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:abb-robotics-safety-controller-configuration sc_cfg.1.00.01.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" version="1.00.01" xmlns="urn:abb-robotics-safety-controller-configuration">
<ValidationInfo validationDate="2019-08-29T13:45:16" validatedBy="W01 Admin"/>
<LockInfo lockDate="2020-02-11T13:54:07" lockedBy="W01 Admin" controllerId="6700-110323"/>
<ConfigurationSeal checksum="776890E3E367E5171CC3FD52700BE8FA5373422BCC6C4214C0321E6BEC02EA52" creationDate="2019-08-29T13:39:23.2736126+02:00" createdBy="W01 Admin" systemName="00RZ21_ST010_IR001" swVersion="1.02.04">
<ProtectedElements checksum="20E318119904F2A8506AD3A00035BE2FE562FA772D371DCC43B04A2E4622550F" />
<SiosCfg version="1.0">
<Net name="PNFDevice">
<Device name="PNFDeviceDev" inSizeBits="64" outSizeBits="64" address="">
<Module name="InputOutputModule" inputSize="8" outputSize="8" sourceAddress="100" destAddress="144" wdTimeout="500" crcLength="3" fParVersion="2" slot="3" silLevel="2" visible="true" readonly="false" />

var protectedElements = configInfoElement.Element(ns + "ProtectedElements");
var protectedElementsAttr = protectedElements.Attribute("checksum");
On the first line we're looking under configInfoElement rather than doc.Root, and in the second line we're looking under protectedElements rather than configInfoElement

Related

Get all value of attribute from each Element from XML

I have an xml file which looks like this:
<HeadercardUnit EndTime="2065-25-45 20:32:44" StartTime="2065-25-45 20:32:23" Rejects="NO" MilliSec="1" Currency="USD" DeclaredDepositAmount="0" denomvalue="1" DepositID="" CustomerID="" HeaderCardID="">
<Counter Number="2" Currency="USD" Output="Stacked" Quality="Accepted" Issue="2006" Value="5" DenomID="" DenomName="5 USD-2006"/>
<Counter Number="31" Currency="USD" Output="Stacked" Quality="Accepted" Issue="2000" Value="1" DenomID="" DenomName="1 USD-2000"/>
<Sum Number="33" Currency="USD" Output="Stacked" Sum="41.00"/>
</HeadercardUnit>
I try to parse it with this code:
string[] content = Directory.GetFiles(Directory.GetCurrentDirectory() + #"\", "*.xml");
XDocument xdoc = XDocument.Load(content[0]);
XElement xml1 = XElement.Load(content[0]);
string xml2 = xml1.ToString();
//Console.WriteLine(xml2);
XElement xml = XElement.Parse(xml2);
var counter = xdoc.Descendants("Counter").Count();
var data = from bps in xdoc.Root.Descendants("Machine")
let Param = bps.Element("ParameterSection")
let Opt = Param?.Element("Operator")
let Hcl = Param?.Element("HeadercardUnit")
let Count = Hcl?.Element("Counter")
select new
{
Type = (string)bps.Attribute("Type"),
SerialNum = (string)bps.Attribute("SerialNumber"),
Startime = (string)Param?.Attribute("StartTime"),
Endtime = (string)Param?.Attribute("EndTime"),
Opt = (string)Opt?.Value,
Number = (string)Count?.Attribute("Number")
};
foreach (var pcl in data)
{
MessageBox.Show(counter.ToString());
for (int i = 0; i < counter; i++)
{
LogService(string.Format("{0},{1},{2},{3},{4},{5}",
pcl.Type, pcl.SerialNum, pcl.Startime, pcl.Endtime, pcl.Opt, pcl.Number));
}
}
The result only give me one line which is looping two time because the counter tag have two elements looks like this:
BPSC1,309322,2065-25-45 20:32:23,2065-25-45 20:32:44,User1,2
BPSC1,309322,2065-25-45 20:32:23,2065-25-45 20:32:44,User1,2
It's a little hard to give a definite answer given you've omitted a portion of the XML that would allow this to be reproduced. However, this line:
let Count = Hcl?.Element("Counter")
Gets the first Counter element. If you want all of them (as you suggest), then you need to iterate through those:
from Count in Hcl.Elements("Counter")
This will then create an object in data for each Counter element.
i added the line inspired by #Charles Mager
from bps in xdoc.Root.Descendants("Machine")
from Countx in bps.Descendants("Counter")
then i can call all attributes

How to seperate strings from a serialized XML node

I have an serialized XML file. This shows the relevant part:
I am reading this XML file with this (code snippet):
temp = Path.GetFileNameWithoutExtension(s);
var document = new XmlDocument();
document.Load(s);
var root = document.DocumentElement;
var node = root["ScenarioDescription"];
var text = node?.InnerText;
var ArmyNode = root["ArmyFiles"];
var ArmyText = ArmyNode?.InnerText;
However, ArmyText returns the concatenation of all three strings that make up the ArmyFiles node. I need them as three separate strings. How can I do this?
This code works to read all the strings in the node and place them into a list:
foreach (XmlElement A in ArmyNode)
{
var ArmyTemp = A.InnerText;
ArmyList.Add(ArmyTemp);
}
var ArmyText = ArmyNode?.InnerText;

C# deserialize xml using linq

I have the following xml file
<?xml version="1.0" encoding="utf-8"?>
<Launchpad>
<Shortcuts>
<Shortcut Id="1">
<Type>Folder</Type>
<FullPath>C:\bla\bla\bla</FullPath>
<Name>Proximity</Name>
</Shortcut>
<Shortcut Id="2">
<Type>Folder</Type>
<FullPath>C:\bla</FullPath>
<Name>Visual Studio 2017</Name>
</Shortcut>
</Shortcuts>
</Launchpad>
I am trying to deserialize to an object like this: (Tried query syntax first, didn't work either)
XDocument xd = XDocument.Load(FullPath);
// query syntax
//var shortcuts = (from s in xd.Descendants("Shortcuts")
// select new Shortcut()
// {
// Id = Convert.ToInt32(s.Attribute("Id")),
// TypeOfLink = GetTypeFromString(s.Descendants("Type")
// .First()
// .Value),
// FullPathToTarget = s.Descendants("FullPath")
// .First()
// .Value,
// Name = s.Descendants("Name").First().Value,
// }).ToList();
// method syntax
List<Shortcut> shortcuts = xd.Descendants("Shortcuts")
.Select(s =>
new Shortcut()
{
//Id = Convert.ToInt32(s.Attribute("Id")),
TypeOfLink = GetTypeFromString(s.Descendants("Type")
.First().Value),
FullPathToTarget = s.Descendants("FullPath")
.First().Value,
Name = s.Descendants("Name")
.First().Value,
}).ToList();
return shortcuts;
For some reason I only get a single shortcut object in the list. Also, for some reason, the s.Attribute("Id") is null.
If anyone has any suggestions to improve the linq query or why the attribute is not working it would be a great help
You have to read .Descendants("Shortcut") instead of .Descendants("Shortcuts").
Something like this:
List<Shortcut> shortcuts = xd.Descendants("Shortcut").Select(s =>
new Shortcut()
{
Id = s.Attribute("Id").Value,
TypeOfLink = s.Descendants("Type").First().Value,
FullPathToTarget = s.Descendants("FullPath").First().Value,
Name = s.Descendants("Name").First().Value,
}).ToList();
I get the full list by selecting the Shortcut as a descendant of Shortcuts.
Also, ID is an XAttribute so you cant convert it to int.
You need to use Value to get the attributes value.
XDocument xd = XDocument.Load(ms);
XElement root = xd.Document.Root;
var list = root.Descendants("Shortcuts").Descendants("Shortcut").Select(x =>
new Shortcut()
{
Id = Convert.ToInt32(x.Attribute("Id").Value),
TypeOfLink = GetTypeFromString(x.Descendants("Type")
.First().Value),
FullPathToTarget = x.Descendants("FullPath")
.First().Value,
Name = x.Descendants("Name").First().Value
}).ToList();

How to extract xml child element

I am trying to figure out the code to extract xml child (I think this is worded correctly) elements. I have searched and tried many samples but cannot find how to drill down to pick out the section I want and return the information I need. Maybe I all I need is someone to define the data I am trying to pull so I can read up on the issue, of course any code would be very helpful and I will figure it out from there. Thanks in advanced for any help!
Here is the xml file. I am trying to run an if statement to find the section named <STATISTICTYPE>PVCAP_CharactersSaved</STATISTICTYPE> and return the <JOBNAME>,<TIMEDELTA>,<VALUESUM>.
<?xml version="1.0" encoding="utf-8"?>
<PVCAPTURESTATISTICCONTAINTER xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<PVCAPTUREJOBSTATISTICS>
<PVCAPTURESTATISTICSUMMARY>
<STATISTICTYPE>PVCAP_CharactersSaved</STATISTICTYPE>
<STATISTICNAME>Characters saved</STATISTICNAME>
<JOBID>24</JOBID>
<JOBNAME>HEAT FILES</JOBNAME>
<TIMEDELTA>422</TIMEDELTA>
<VALUESUM>25432</VALUESUM>
</PVCAPTURESTATISTICSUMMARY>
<PVCAPTURESTATISTICSUMMARY>
<STATISTICTYPE>PVCAP_CharactersSaved_NoMM</STATISTICTYPE>
<STATISTICNAME>Characters saved (no match and merge)</STATISTICNAME>
<JOBID>24</JOBID>
<JOBNAME>HEAT FILES</JOBNAME>
<TIMEDELTA>422</TIMEDELTA>
<VALUESUM>25432</VALUESUM>
</PVCAPTURESTATISTICSUMMARY>
</PVCAPTUREJOBSTATISTICS>
<DOCUMENTCOUNT>762</DOCUMENTCOUNT>
<PAGECOUNT>3194</PAGECOUNT>
<IMAGECOUNT>3194</IMAGECOUNT>
<VERSION>2.0</VERSION>
</PVCAPTURESTATISTICCONTAINTER>
You can use LINQ to XML, particularly the XElement class.
var element = XElement.Parse(xmlStr).Element("PVCAPTUREJOBSTATISTICS")
.Elements("PVCAPTURESTATISTICSUMMARY")
.First(c => c.Element("STATISTICTYPE").Value == "PVCAP_CharactersSaved")
var jobName = element.Element("JOBNAME").Value;
var timeDelta = element.Element("TIMEDELTA").Value;
var valueSum = element.Element("VALUESUM").Value;
You'll want to add in some error handling and whatnot here, but this should get you going in the right direction.
You can do something like this:
XElement res = XElement.Parse(xmlResult);
foreach(var elem in res.Element("PVCAPTUREJOBSTATISTICS").Elements("PVCAPTURESTATISTICSUMMARY"))
{
if (elem.Element("STATISTICTYPE").Value.Equals("PVCAP_CharactersSaved", StringComparison.Ordinal))
{
string jobName = elem.Element("JOBNAME").Value;
string timeDelta = elem.Element("TIMEDELTA").Value;
string valueSum = elem.Element("VALUESUM").Value;
}
}
You can use XDocument and LINQ-to-XML to do that quite easily, for example :
string xml = "your xml content here";
XDocument doc = XDocument.Parse(xml);
//or if you have the xml file instead :
//XDocument doc = XDocument.Load("path_to_xml_file.xml");
var result = doc.Descendants("PVCAPTURESTATISTICSUMMARY")
.Where(o => (string) o.Element("STATISTICTYPE") == "PVCAP_CharactersSaved")
.Select(o => new
{
jobname = (string) o.Element("JOBNAME"),
timedelta = (string) o.Element("TIMEDELTA"),
valuesum = (string) o.Element("VALUESUM")
});
foreach (var r in result)
{
Console.WriteLine(r);
}

Parsing specific part of XML file

I have a .gpx XML file with the following sample:
<trk>
<name>Test</name>
<trkseg>
<trkpt lon="-84.89032996818423" lat="32.75810896418989">
<ele>225.0</ele>
<time>2011-04-02T11:57:48.000Z</time>
<extensions>
<gpxtpx:TrackPointExtension>
<gpxtpx:cad>0</gpxtpx:cad>
</gpxtpx:TrackPointExtension>
</extensions>
</trkpt>
</trkseg>
</trk>
I'm using Linq to XML to parse this but I'm having a difficult time parsing the extensions section. Here's the code I'm using:
var gpxDoc = LoadFromStream(document);
var gpx = GetGpxNameSpace();
var gpxtpx = XNamespace.Get("gpxtpx");
var tracks = from track in gpxDoc.Descendants(gpx + "trk")
select new
{
Name = DefaultStringValue(track, gpx, "name"),
Description = DefaultStringValue(track, gpx, "desc"),
Segments = (from trkSegment in track.Descendants(gpx + "trkseg")
select new
{
TrackSegment = trkSegment,
Points = (from trackpoint in trkSegment.Descendants(gpx + "trkpt")
select new
{
Lat = Double(trackpoint.Attribute("lat").Value),
Lng = Double(trackpoint.Attribute("lon").Value),
Ele = DefaultDoubleValue(trackpoint, gpx, "ele"),
Time = DefaultDateTimeValue(trackpoint, gpx, "time"),
Extensions = (
from ext in trackpoint.Descendants(gpx + "extensions").Descendants(gpxtpx + "TrackPointExtension")
select new
{
Cad = DefaultIntValue(ext, gpxtpx, "cad")
}).SingleOrDefault()
})
})
};
Here's the relevant helper code:
private static double? DefaultIntValue(XContainer element, XNamespace ns, string elementName)
{
var xElement = element.Element(ns + elementName);
return xElement != null ? Convert.ToInt32(xElement.Value) : (int?)null;
}
private XNamespace GetGpxNameSpace()
{
var gpx = XNamespace.Get("http://www.topografix.com/GPX/1/1");
return gpx;
}
The actual error I'm getting is
The following error occurred: Object reference not set to an instance of an object.
and it bombs on this code:
Extensions = (from ext in trackpoint.Descendants(gpx + "extensions").Descendants(gpxtpx + "TrackPointExtension")
select new
{
Cad = DefaultIntValue(ext, gpxtpx, "cad")
}).SingleOrDefault();
I just don't know how to fix it.
Since you never declare the namespace (xmlns:gpxtpx="http://www.topografix.com/GPX/1/1") it is never going to match. The xml fragment you provided is not well formed due to the lack of the namespace.
If the fragment posted is snipped from a larger document, consider switching to XML API's rather than string manipulation. If that is the entirety of the XML you receive from an outside system, add it to a root node which you can declare the schema in:
<root xmlns:gpxtpx="http://www.topografix.com/GPX/1/1">
<!-- put your xml fragment here -->
</root>

Categories