Get list of line items from XML - c#

I have an eConnect outgoing document pulled from MSMQ, i need to loop through the line items.
I've tried:
XmlNodeList nodes = root.SelectNodes("/Sales_History_Transaction/eConnect/SO_Hist_Trans/Line");
(and several other attempts) with no success...
Here is the XML, how can i get a collection of Line items from the "Line" nodes so that i can then get the line item details?
<Sales_History_Transaction:root>
<eConnect ACTION="1" Requester_DOCTYPE="Sales_History_Transaction" DBNAME="TWO" TABLENAME="SOP30200" DATE1="2013-05-03T09:24:09.970" SOPNUMBE="999999" SOPTYPE="3">
<SO_Hist_Trans>
<SOPNUMBE>999999</SOPNUMBE>
<SOPTYPE>3</SOPTYPE>
<Line>
<CMPNTSEQ>0</CMPNTSEQ>
<LNITMSEQ>998777</LNITMSEQ>
<ITEMNMBR>0099999</ITEMNMBR>
<ITEMDESC>Laptop</ITEMDESC>
</Line>
<Line>
<CMPNTSEQ>0</CMPNTSEQ>
<LNITMSEQ>777</LNITMSEQ>
<ITEMNMBR>0099</ITEMNMBR>
<ITEMDESC>Desktop</ITEMDESC>
</Line>
<Line>
<CMPNTSEQ>0</CMPNTSEQ>
<LNITMSEQ>679777</LNITMSEQ>
<ITEMNMBR>0569</ITEMNMBR>
<ITEMDESC>Memory</ITEMDESC>
</Line>
</SO_Hist_Trans>
</eConnect>
</Sales_History_Transaction:root>

Your xml is not well-formed.
The root tag seems to consist of an undeclared namespace Sales_History_Transaction and the element name root. Have you missed the line in which Sales_History_Transaction is defined?
Once you have valid xml, it should be as simple (depending on namespaces) as:
var xdoc = XDocument.Parse(yourXml);
var nodes = xdoc.Descendants("Line");

Does this do the trick for your example? Or is this the same you tried that failed?
XmlDocument doc1 = new XmlDocument();
doc1.Load("yoururl"); //I don't know from where you load
XmlElement root = doc1.DocumentElement;
XmlNodeList nodes = root.SelectNodes("/Sales_History_Transaction/eConnect/SO_Hist_Trans/Line");
foreach (XmlNode node in nodes) {
Console.Out.WriteLine(node["CMPNTSEQ"].InnerText);
Console.Out.WriteLine(node["LNITMSEQ"].InnerText);
Console.Out.WriteLine(node["ITEMNMBR"].InnerText);
Console.Out.WriteLine(node["ITEMDESC"].InnerText);
Console.Out.WriteLine("------------------------");
}

Figured it out:
XmlNodeList nodes = xmlDocument.GetElementsByTagName("Line");
foreach (XmlNode node in nodes)
{
string txt = node["ElementName"].InnerText;
}
this enumerates through all the "Line" elements in the XML.

Related

Extracting nested nodes from xml file c#

I have this xml file:
<table head="Film">
<row>
<id>USD</id><jan>Jan</jan><feb>Feb</feb><mar>Mar</mar><apr>Apr</apr><maj>May</maj><jun>Jun</jun><jul>Jul</jul><aug>Aug</aug><sep>Sep</sep><okt>Oct</okt><nov>Nov</nov><dec>Dec</dec><sum>Year</sum>
</row>
<row>
<id>2018</id><jan>7629</jan><feb>6433</feb><mar>5573</mar><apr>3676</apr><maj>2545</maj><jun>2542</jun><jul>266</jul><aug>276</aug><sep>2690</sep><okt>371</okt><nov>5446</nov><dec>754</dec><sum>52731</sum>
</row>
I'm trying to extract the individual values for every month.
I've tried
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("model.xml");
XmlNodeList nodeList = xmlDoc.GetElementsByTagName("table");
foreach (XmlNode node in nodeList) // for each <testcase> node
{
Console.WriteLine(node["row"].InnerText);
}
This gives an exception because node["row"] is empty.
Any ideas?
Firstly your XML is not valid. You need to have a </table> on there.
//In this example GetXml() just returns your XML
var doc = XDocument.Parse(GetXml());
var rows = doc.Descendants("table").Elements("row").ToList();
foreach(var element in rows[1].Elements()){
Console.WriteLine(element?.Value);
}
Now this is just a basic example based of your XML. You would likely want it to be more robust. You will notice I am showing you this with LINQ, I feel it's more readable than XmlDocument.
You need to loop through the child nodes to get your desired result as follow:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("model.xml");
XmlNodeList nodeList = xmlDoc.GetElementsByTagName("table");
foreach (XmlNode node in nodeList) // for each <testcase> node
{
foreach (XmlNode row in node.ChildNodes)
{
foreach (XmlNode mon in row.ChildNodes)
{
}
}
}

C# XML to dropdownBox

Returned XML from URL:
<root>
<APIVersion>0.1</APIVersion>
<resource>persons</resource>
<search>givenname</search>
<query>andreas</query>
<limit>400</limit>
<results>
<item>
<persons>
<personId>21168</personId>
<givenName>Andreas</givenName>
<familyName>Garpe</familyName>
<email>andreas.garpe#t-fk.no</email>
<mobilePhone/>
<workPhone/>
<positions>...</positions>
</persons>
</item>
<item>...</item>
<item>...</item>
<item>...</item>
</results>
</root>
(Keep in mind that "item" is the objects with personnel info.)
I have a textbox defined as bunifuTextbox1.
I input a name, and it returns the names from the returned XML result and put all the names returned into a dropdown box.
private void button1_Click(object sender, EventArgs e)
{
string address = "http://ws.t-fk.no/?resource=persons&search=givenname&string=" + bunifuTextbox1.text;
XmlDocument doc1 = new XmlDocument();
doc1.Load(address);
XmlElement root = doc1.DocumentElement;
XmlNodeList nodes = root.SelectNodes("/results/item");
foreach (XmlNode node in nodes)
{
string tempf = node["persons"]["givenName"].InnerText;
bunifuDropdown1.AddItem(tempf);
}
}
I'm not sure why this doesen't work. Any help?
Your XPath is incorrect.
Instead of
XmlNodeList nodes = root.SelectNodes("/results/item");
try
XmlNodeList nodes = root.SelectNodes("results/item");
or
XmlNodeList nodes = root.SelectNodes("./results/item");
or
XmlNodeList nodes = root.SelectNodes("//results/item");
Use "results/item" or "./results/item" for an item element that is a child of a results element that is a child of the root node.
Using "//results/item" will select item elements that are children of results elements where the results element is anywhere in the XML.

Can't find a certain node in XML

My problem is that I can't seem to get a list of nodes I need. I have tried multiple solutions and it doesn't seem to work. This is a part of my xml file:
<findItemsByCategoryResponse xmlns="http://www.ebay.com/marketplace/search/v1/services">
<ack>Success</ack>
<version>1.13.0</version>
<timestamp>2016-08-23T07:33:22.497Z</timestamp>
<searchResult count="100">
<item>
<itemId>152210133431</itemId>
<title>...</title>
<globalId>EBAY-GB</globalId>
<primaryCategory>
<categoryId>218</categoryId>
<categoryName>Magic the Gathering</categoryName>
</primaryCategory>
<galleryURL>
http://thumbs4.ebaystatic.com/m/meIDrVqhmbpQMYCxzeUvR9Q/140.jpg
</galleryURL>
<viewItemURL>
http://www.ebay.co.uk/itm/MTG-See-Unwritten-Khans-Tarkir-MYTHIC-MINT-/152210133431
</viewItemURL>
<paymentMethod>PayPal</paymentMethod>
<autoPay>false</autoPay>
<location>London,United Kingdom</location>
<country>GB</country>
<shippingInfo>
<shippingServiceCost currencyId="GBP">1.1</shippingServiceCost>
<shippingType>Flat</shippingType>
<shipToLocations>GB</shipToLocations>
</shippingInfo>
<sellingStatus>
<currentPrice currencyId="GBP">0.5</currentPrice>
<convertedCurrentPrice currencyId="GBP">0.5</convertedCurrentPrice>
<bidCount>0</bidCount>
<sellingState>Active</sellingState>
<timeLeft>P0DT0H19M12S</timeLeft>
</sellingStatus>
<listingInfo>
<bestOfferEnabled>false</bestOfferEnabled>
<buyItNowAvailable>false</buyItNowAvailable>
<startTime>2016-08-18T07:52:34.000Z</startTime>
<endTime>2016-08-23T07:52:34.000Z</endTime>
<listingType>Auction</listingType>
<gift>false</gift>
</listingInfo>
<condition>
<conditionId>1000</conditionId>
<conditionDisplayName>New</conditionDisplayName>
</condition>
<isMultiVariationListing>false</isMultiVariationListing>
<topRatedListing>false</topRatedListing>
</item>
</searchResult>
<paginationOutput>...</paginationOutput>
<itemSearchURL>...</itemSearchURL>
</findItemsByCategoryResponse>
Normally there are 100 item nodes in this xml file, I just shorted the file. I tried to get the ''item'' nodes and everything inside in a XmlNodeList, but when I debug it says it contains 0 items. After this I want to retrieve some data, as you can see in the code. Note: I tried a lot of xpath values!
Here is the code I used:
XmlDocument xmlText = new XmlDocument();
xmlText.Load(basicUrl);
XmlElement root = xmlText.DocumentElement;
XmlNodeList xnList = root.SelectNodes("//item");
foreach(XmlNode item in xnList)
{
string title = item["title"].InnerText;
Console.WriteLine(title);
}
XPath Expressions are always namespace aware (if the Element has a Namespace then the XPath must reference it by namespace). Also, in XPath expressions, the 'default' namespace is always in the URI "". In your case you should do something like this :
XmlDocument xmlText = new XmlDocument();
xmlText.Load(yourXml);
XmlElement root = xmlText.DocumentElement;
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlText.NameTable);
nsmgr.AddNamespace("t", "http://www.ebay.com/marketplace/search/v1/services");
XmlNodeList xnList = xmlText.SelectNodes("//t:item", nsmgr);
foreach (XmlNode item in xnList)
{
string title = item["title"].InnerText;
Console.WriteLine(title);
}
SelectNodes with namespace :
https://msdn.microsoft.com/en-US/library/4bektfx9(v=vs.110).aspx

Selecting Particular Node List in XML

<Report xmlns="Microsoft.SystemCenter.DataWarehouse.Report.Alert" xmlns:p1="w3.org/2001/XMLSchema-instance"; Name="Microsoft.SystemCenter.DataWarehouse.Report.Alert" p1:schemaLocation="Microsoft.SystemCenter.DataWarehou?Schema=True">
<Title>Alert Report</Title>
<Created>6/27/2013 9:32 PM</Created>
<StartDate>6/1/2013 9:29 PM</StartDate>
<EndDate>6/27/2013 9:29 PM</EndDate>
<TimeZone>(UTC)</TimeZone>
<Severity>Warning, Critical</Severity>
<Priority>Low, Medium, High</Priority>
<AlertTable>
<Alerts>
<Alert>
<AlertName></AlertName>
<Priority></Priority>
</Alert>
</Alerts>
</AlertTable>
</Report>
So I'm trying to pull down the list of nodes that appear under Alerts child. So /Report/AlertTable/Alerts.
I've done very similar before but in this format it is not working for some reason. Can someone point me out in the right direction?
XmlDocument Log = new XmlDocument();
Log.Load("test.xml");
XmlNodeList myLog = Log.DocumentElement.SelectNodes("//Report/AlertTable/Alerts");
foreach (XmlNode alert in myLog)
{
Console.Write("HERE");
Console.WriteLine(alert.SelectNodes("AlertName").ToString());
Console.WriteLine(alert.SelectNodes("Priority").ToString());
Console.Read();
}
EDIT:
One of the responses had me try to use a bunch of namespace with p1 but had no such luck.
EDIT:
Did not work either:
var name = new XmlNamespaceManager(log.NameTable);
name.AddNamespace("Report", "http://www.w3.org/2001/XMLSchema-instance");
XmlNodeList xml = log.SelectNodes("//Report:Alerts", name);
From a site:
nodename Selects all nodes with the name "nodename"
/ Selects from the root node
// Selects nodes in the document from the current node that match the selection no matter where they are
So I believe
"/AlertTable/Alerts"
would work, as that would be 'from the root node' as well as
"Report/AlertTable/Alerts"
XPath Site
Figured this sucker out.
It had to do with the namespace of "Microsoft.SystemCenter.DataWarehouse.Report.Alert". Changing this to anything but that won't read the XML properly.
XmlDocument log = new XmlDocument();
log.Load(#"C:\Users\barranca\Desktop\test.xml");
// XmlNodeList xml = log.SelectNodes("//ns1:Alerts");
var name = new XmlNamespaceManager(log.NameTable);
name.AddNamespace("ns1", "Microsoft.SystemCenter.DataWarehouse.Report.Alert");
XmlNodeList xml = log.SelectNodes("//ns1:Alert", name);
foreach (XmlNode alert in xml)
{
Console.Write("HERE");
XmlNode test = alert.SelectSingleNode("//ns1:AlertName",name);
string testing = test.InnerText;
Console.Write(testing);
}

Remove all Artist node from an XML file

I have an XmlWriter that contains a xml that looks like the one below, just with a lot more nodes. what's the fastest and best way to remove all the ARTIST node from this xml ?
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
</CD>
<CD>
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tyler</ARTIST>
</CD>
</CATALOG>
As long as the file isn't gigabytes XmlDocument should be fine:
XmlDocument XDoc = new XmlDocument();
XDoc.Load(MapPath(#"~\xml\test.xml"));
XmlNodeList Nodes = XDoc.GetElementsByTagName("ARTIST");
foreach (XmlNode Node in Nodes)
Node.ParentNode.RemoveChild(Node);
XDoc.Save(MapPath(#"~\xml\test_out.xml"));
When I tried Steve's solution I received the following error "The element list has changed. The enumeration operation failed to continue". I know it is a bit of a hack but to get around this I used the following:
//Load XML
XmlDocument XDoc = new XmlDocument();
XDoc.Load(MapPath(#"~\xml\test.xml"));
//Get list of offending nodes
XmlNodeList Nodes = XDoc.GetElementsByTagName("ARTIST");
//Loop through the list
while (Nodes.Count != 0) {
foreach (XmlNode Node in Nodes) {
//Remove the offending node
Node.ParentNode.RemoveChild(Node); //<--This line messes with our iteration and forces us to get a new list after each remove
//Stop the loop
break;
}
//Get a refreshed list of offending nodes
Nodes = XDoc.GetElementsByTagName("ARTIST");
}
//Save the document
XDoc.Save(newfile);
I was in a bind and needing something quick and this got the job done.
From the way your question is worded, I'm assuming that you have an XmlWriter that has been used to write an XmlDocument and you want to manipulate what it is going to write before flush is called.
I'm afraid that what you're trying to do may be impossible. The XmlWriter is not meant to manipulate XML before it is written to a destination stream.

Categories

Resources