C# XMLNodeList Item Children - c#

I'm currently working on importing XML feed data into our CMS, but am struggling in how to tackle this issue.
For a course, it can have up to 9 staff members looking after it. Each having a name, phone number, email address and type of job. In the CMS for the import, i have 9 sets of these 4 fields, in order to save the data. To get around this, if there is only 5 staff out of 9, i'm targeting an XMLnodelist of course contacts - if it's null, have an empty string, otherwise populate fields.
In the code below, cclist is defined as an xmlnodelist, and test1 gets the full contents of International Admissions as a whole as a string.
What i want to be able to do, using the same format expanded, is for up to 9 course contacts, get the name, email, phone etc of the xmlnodelist item and separate them out, so they can be targeted for the import. I've tried using descendants etc, but can't seem to be able to target these individually, instead of a full string.
Any help would be greatly appreciated. Thanks.
XML
<course>
<dc:description>...</dc:description>
<dc:title>
<![CDATA[ Marketing ]]>
</dc:title>
<learningOutcome>...</learningOutcome>
<test:coursecontacts
<test:contact xsi:type="LeadAcademic">
<test:name
<![CDATA[ Tommy Thompson ]]>
</test:name
<test:phone>0123456789</test:phone>
<test:email>tt#test.com</test:email>
</test:contact>
<test:contact xsi:type="Admissions Administrator">
<test:name>
<![CDATA[ International Admissions ]]>
</test:name>
<test:phone>0123456678</test:phone>
<test:email>ia#test.com</test:email
</test:contact
<test:contact xsi:type="Course Leader">...</test:contact>
<test:contact xsi:type="Admissions Administrator">...</test:contact>
<test:contact xsi:type="Course Administrator">...</test:contact>
</test:coursecontacts>
</course>
C#
protected override FeedCourse MapXmlNodeToEntity(XElement p)
{
var xmlResult = new XmlDocument();
xmlResult.LoadXml(p.ToString());
var test = p.ToString();
var xmlnsManager = new XmlNamespaceManager(xmlResult.NameTable);
xmlnsManager.AddNamespace("ns", "http://xcri.org/profiles/1.2/catalog");
xmlnsManager.AddNamespace("xcriTerms", "http://xcri.org/profiles/catalog/terms");
xmlnsManager.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
xmlnsManager.AddNamespace("xhtml", "http://www.w3.org/1999/xhtml");
xmlnsManager.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");
xmlnsManager.AddNamespace("dcterms", "http://purl.org/dc/terms/");
xmlnsManager.AddNamespace("credit", "http://purl.org/net/cm");
xmlnsManager.AddNamespace("mlo", "http://purl.org/net/mlo");
xmlnsManager.AddNamespace("courseDataProgramme", "http://xcri.co.uk");
xmlnsManager.AddNamespace("test", "http://www.test.com");
var elements = xmlResult.ChildNodes;
var title = xmlResult.DocumentElement.SelectSingleNode("dc:title", xmlnsManager).InnerText;
var description = xmlResult.DocumentElement.SelectSingleNode("dc:description", xmlnsManager).InnerText;
var learningOutcome = xmlResult.DocumentElement.SelectSingleNode("ns:learningOutcome", xmlnsManager).InnerText;
XmlNodeList ccList = xmlResult.DocumentElement.SelectNodes("test:coursecontacts/test:contact", xmlnsManager);
var test1 = ccList.Item(1).InnerText;
// Above line gets full contents of test:contact, but i need down to levels of xsi:type, name, phone and email
}

Had a break away, came back and decided to try a 2d array for this solution. Allows me to get the value i need, so i'm happy. Thanks anyway, and hope this helps someone else.
int contactcount = 0;
String[,] contactsArray = new String[9, 3];
XmlNodeList ccList = xmlResult.DocumentElement.SelectNodes("test:coursecontacts/test:contact", xmlnsManager);
var test1 = ccList.Item(1).InnerText;
foreach (XmlNode xn in ccList)
{
contactsArray[contactcount, 0] = xn["test:name"].InnerText + " ("+ xn["#xsi:type"]+")";
contactsArray[contactcount, 1] = xn["test:phone"].InnerText;
contactsArray[contactcount, 2] = xn["test:email"].InnerText;
contactcount++;
}
var test2 = contactsArray[0,0].ToString()+ contactsArray[0, 1].ToString() + contactsArray[0, 2].ToString();
All the best,
Andy.

Related

XML parsing a subtree in C#

I have many xml-files which I need to parse. The xml-files are loaded from elsewhere. I can examine these files to get the paths I need to extract my desired data. The paths aren't the same.
So I added the paths in an ini-file for each xml-file. This works fine for 5 of 6 files.
WebClient client = new WebClient();
data = client.DownloadData("ftp://some.site/my.xml");
MemoryStream stream = new MemoryStream(data);
XmlDocument xml_doc = new XmlDocument();
xml_doc.Load(stream);
var prod_ids = xml_doc.DocumentElement.SelectNodes("/Catalog/Products/Product/Product_Id/text()");
foreach (XmlNode node in prod_ids) {
[...]
}
In the last file I need to get 2 information from one subtree at once, because I have to combine them in one string, therefore reading all nodes seperatly doesn't work. See Example-XML:
<Catalog>
<Created><![CDATA[2020-11-16T00:22:11+01:00]]></Created>
<Products>
<Product>
<Product_Id><![CDATA[ABC]]></Product_Id>
<Color_Code><![CDATA[123]]></Color_Code>
<Size><![CDATA[]]></Size>
<Length>210</Length>
<Width>0</Width>
</Product>
<Product>
<Product_Id><![CDATA[ABC]]></Product_Id>
<Color_Code><![CDATA[456]]></Color_Code>
<Size><![CDATA[]]></Size>
<Length>44</Length>
<Width>55</Width>
</Product>
<Product>
<Product_Id><![CDATA[XYZ]]></Product_Id>
<Color_Code><![CDATA[123]]></Color_Code>
<Size><![CDATA[]]></Size>
<Length>150</Length>
<Width>11</Width>
</Product>
</Products>
</Catalog>
I'm lookig for some code which parses each subtree (/Catalog/Products/Product) in which I can read the innerText from Product_Id and Color_Code to combine them to one string.
Any ideas?
You're really close, but you're going too low in the DOM tree. Instead of looping through each Product/ProductID, start your loop at each Product, then in the loop get each ProductID / ColorCode.
foreach( XmlElement ndProduct in xml.SelectNodes( "//Product") ) {
XmlElement ndProductID = (XmlElement)ndProduct.SelectSingleNode("Product_Id");
string strProductID = ndProductID.InnerText;
XmlElement ndColorCode = (XmlElement)ndProduct.SelectSingleNode("Color_Code");
string strColorCode = ndColorCode.InnerText;
string strReturn = strProductID + " - " + strColorCode;
}
Use a more modern linq to xml.
var doc = XDocument.Load(stream);
var values = doc.Root
.Element("Products")
.Elements("Product")
.Select(p => p.Element("Product_Id").Value + p.Element("Color_Code").Value);
foreach (var value in values)
Console.WriteLine(value);
I can offer the following solution.
Get the values of different nodes using the OR operation |.
Then we go through the collection with an increment of two and combine the values.
var prod_ids = xml_doc.DocumentElement.SelectNodes(
"/Catalog/Products/Product/Product_Id | /Catalog/Products/Product/Color_Code");
for (int i = 0; i < prod_ids.Count; i += 2)
Console.WriteLine(prod_ids[i].InnerText + prod_ids[i + 1].InnerText);

Create loops and parsing XML nodes value for Selenium tests

I am trying to write a test function in C# that read data from an XML file and parse into Selenium testing methods , the XML code is like:
<home>
<ask_frame>
<button>
<id>Object ID<id>
<xpath>Object XPath<xpath>
<textbox>
<id>Object ID<id>
<xpath>Object XPath<xpath>
</ask_frame>
<search_frame>
<id>Object ID<id>
<xpath>Object XPath<xpath>
</search_frame>
<home>
I am trying to create a loop that read the id and xpath value from these nodes and parse them into an method for searching a webpage element by id and xpath. My initial attempt was:
Code updated
public void CheckIdTest()
{
driver.Navigate().GoToUrl(baseURL + "FlightSearch");
XmlDocument xd = new XmlDocument();
xd.Load(#"C:\XMLFile1.xml");
XmlNodeList mainlist = xd.SelectNodes("//home/*");
XmlNode mainroot = mainlist[0];
foreach (XmlNode xnode in mainroot)
{
string objID = xnode.SelectSingleNode("id").InnerText;
string objXPath = xnode.SelectSingleNode("XPath").InnerText;
objID = objID.Trim();
objXPath = objXPath.Trim();
String checkValue = "ObjID value is: " + objID + Environment.NewLine+ "ObjXPath value is: " + objXPath;
System.IO.File.WriteAllText(#"C:\checkvalue.txt", checkValue);
objectCheck(objXPath, objID);
}
}
I have put a String and checked that correct values for ObjID and ObjXPath have been achieved, but this loop also went only twice (checked 2 nodes in first branch). How could I make it runs through every node in my XML?
Any suggestions and explanations to the code will be highly appreciated.
Basically these two lines are using incorrect XPath :
XmlNodeList idlist = xd.SelectNodes("id");
XmlNodeList xpathlist = xd.SelectNodes("XPath");
<id> and <xpath> nodes aren't located directly at the root level, so you can't access it just like above. Besides, xpath is case-sensitive so you should've used "xpath" instead of "XPath". Try to fix it like this :
XmlNodeList idlist = xd.SelectNodes("//id");
XmlNodeList xpathlist = xd.SelectNodes("//xpath");
or more verbose :
XmlNodeList idlist = xd.SelectNodes("home/*/id");
XmlNodeList xpathlist = xd.SelectNodes("home/*/xpath");
UPDATE :
Responding to your comment about looping problem, I think you want to change it like this :
foreach (XmlNode xnode in mainroot.ChildNodes)
{
string objID = xnode.SelectSingleNode("id").InnerText;
string objXPath = pathroot.SelectSingleNode("xpath").InnerText;
objectCheck(objID, objXPath);
}
You are getting this error because you are trying to use an object that is null i.e not instantiated.
Put in a breakpoint at the line
XmlDocument xd = new XmlDocument();
and step through line by line till you find where the nothing.null reference is.
It should not take long to find out what the problem is.

Getting at data in a file

Okay, silly question here, but I'm just starting with xml.
<fifth points = '500' answer = 'Ada Lovelace'>
This woman, known as the world's first computer programmer
was also a Countess.
</fifth>
How exactly do I get at the data after Ada Lovelace? I understand that fifth is the node, and that points and answer are the attributes. What must I grab to get the desired data?
here is something you can try and test to help you understand how to get at the node.InnerText
var testDoc =
#"<fifth points = '500' answer = 'Ada Lovelace'>"
+ "This woman, known as the world's first computer programmer "
+ "was also a Countess."
+ "</fifth>";
XmlDocument docXML = new XmlDocument();
docXML.LoadXml(testDoc);
var innerxml = docXML.InnerText;
MessageBox.Show(innerxml);
You're probably looking for something like:
node.InnerText
To learn about parsing nodes XML in C#, read up on http://www.csharp-examples.net/xml-nodes-by-name/ . This post, http://www.codeproject.com/Articles/7718/Using-XML-in-C-in-the-simplest-way may also be helpful as well, as it provides some of the simplest ways of parsing XML in C#.
You can simply index XmlNode with the node name: xmlNode["FirstName"].InnerText. See the example below.
XmlDocument xml = new XmlDocument();
// suppose that myXmlString contains "<Names>...</Names>":
xml.LoadXml(myXmlString);
XmlNodeList xnList = xml.SelectNodes("/Names/Name");
foreach (XmlNode xn in xnList)
{
string firstName = xn["FirstName"].InnerText;
string lastName = xn["LastName"].InnerText;
Console.WriteLine("Name: {0} {1}", firstName, lastName);
}
The output is:
Name: John Smith Name: James White
Edit: It's important to note that DJ KRAZE and Jeremy Thompson also inspired my answer.
You can use Linq to Xml. If this is a full xml you need to parse:
var xml = "<fifth points='500' answer='Ada Lovelace'>This woman, known as the world's first computer programmer was also a Countess.</fifth>";
XElement element = XElement.Parse(xml);
string text = (string)element; // takes element's innerText
If you need to select this element from your xml file by answer attribute value:
XDocument xdoc = XDocument.Load(path_to_xml_file);
string text = xdoc.Descendants("fifth")
.Where(e => (string)e.Attribute("answer") == "Ada Lovelace")
.Select(e => (string)e)
.FirstOrDefault(); // returns first matched element or null

Get certain xml node and save the value

Considering the following XML:
<Stations>
<Station>
<Code>HT</Code>
<Type>123</Type>
<Names>
<Short>H'bosch</Short>
<Middle>Den Bosch</Middle>
<Long>'s-Hertogenbosch</Long>
</Names>
<Country>NL</Country>
</Station>
</Stations>
There are multiple nodes. I need the value of each node.
I've got the XML from a webpage (http://webservices.ns.nl/ns-api-stations-v2)
Login (--) Pass (--)
Currently i take the XML as a string and parse it to a XDocument.
var xml = XDocument.Parse(xmlString);
foreach (var e in xml.Elements("Long"))
{
var stationName = e.ToString();
}
You can retrieve "Station" nodes using XPath, then get each subsequent child node using more XPath. This example isn't using Linq, which it looks like you possibly are trying to do from your question, but here it is:
XmlDocument xml = new XmlDocument();
xml.Load(xmlStream);
XmlNodeList stations = xml.SelectNodes("//Station");
foreach (XmlNode station in stations)
{
var code = station.SelectSingleNode("Code").InnerXml;
var type = station.SelectSingleNode("Type").InnerXml;
var longName = station.SelectSingleNode("Names/Long").InnerXml;
var blah = "you should get the point by now";
}
NOTE: If your xmlStream variable is a String, rather than a Stream, use xml.LoadXml(xmlStream); for line 2, instead of xml.Load(xmlStream). If this is the case, I would also encourage you to name your variable to be more accurately descriptive of the object you're working with (aka. xmlString).
This will give you all the values of "Long" for every Station element.
var xml = XDocument.Parse(xmlStream);
var longStationNames = xml.Elements("Long").Select(e => e.Value);

Parsing XML document using .Descendents(value)

I am trying to parse an xml document that I have created. However xml.Descendants(value) doesn't work if value has certain characters (including space, which is my problem).
My xml is structured like this:
<stockists>
<stockistCountry country="Great Britain">
<stockist>
<name></name>
<address></address>
</stockist>
</stockistCountry>
<stockistCountry country="Germany">
<stockist>
<name></name>
<address></address>
</stockist>
</stockistCountry>
...
</stockists>
And my C# code for parsing looks like this:
string path = String.Format("~/Content/{0}/Content/Stockists.xml", Helper.Helper.ResolveBrand());
XElement xml = XElement.Load(Server.MapPath(path));
var stockistCountries = from s in xml.Descendants("stockistCountry")
select s;
StockistCountryListViewModel stockistCountryListViewModel = new StockistCountryListViewModel
{
BrandStockists = new List<StockistListViewModel>()
};
foreach (var stockistCountry in stockistCountries)
{
StockistListViewModel stockistListViewModel = new StockistListViewModel()
{
Country = stockistCountry.FirstAttribute.Value,
Stockists = new List<StockistDetailViewModel>()
};
var stockist = from s in xml.Descendants(stockistCountry.FirstAttribute.Value) // point of failure for 'Great Britain'
select s;
foreach (var stockistDetail in stockist)
{
StockistDetailViewModel stockistDetailViewModel = new StockistDetailViewModel
{
StoreName = stockistDetail.FirstNode.ToString(),
Address = stockistDetail.LastNode.ToString()
};
stockistListViewModel.Stockists.Add(stockistDetailViewModel);
}
stockistCountryListViewModel.BrandStockists.Add(stockistListViewModel);
}
return View(stockistCountryListViewModel);
I am wondering if I am approaching the Xml parsing correctly, whether I shouldn't have spaces in my attributes etc? How to fix it so that Great Britain will parse
However xml.Descendants(value) doesn't work if value has certain characters
XElement.Descendants() expects an XName for the tag, not for the value.
And XML tags are indeed not allowed to contain spaces.
Your sample XML however only contains a value for an attribute, and the space there is fine.
Update:
I think you need
//var stockist = from s in xml.Descendants(stockistCountry.FirstAttribute.Value)
// select s;
var stockists = stockistCountry.Descendants("stockist");

Categories

Resources