When an item is bought with Buy It Now and immediate payment, my code is being triggered correctly (the code sends me an email to test it).
What I want is the ebay item id of the item sold which I believe is here (found this sample in the documentation):
<GetItemTransactionsResponse xmlns="urn:ebay:apis:eBLBaseComponents">
<Timestamp>2015-07-02T00:09:03.273Z</Timestamp>
<Ack>Success</Ack>
<Version>967</Version>
<Build>E967_CORE_BUNDLED_11481347_R1</Build>
<PaginationResult>
<TotalNumberOfPages>1</TotalNumberOfPages>
<TotalNumberOfEntries>1</TotalNumberOfEntries>
</PaginationResult>
<HasMoreTransactions>false</HasMoreTransactions>
<TransactionsPerPage>100</TransactionsPerPage>
<PageNumber>1</PageNumber>
<ReturnedTransactionCountActual>1</ReturnedTransactionCountActual>
<Item>
<AutoPay>false</AutoPay>
<Currency>USD</Currency>
**<ItemID>110048746230</ItemID>**
I'm using the eBay API:
public void EbayCall(GetItemTransactionsResponseType response)
Is that the correct object to expect? I can't see why response.ReturnedTransactionCountActual is 0. Also response.Item.ItemID is null.
response.Ack returns Success.
What am I doing wrong?
Looks like you are getting an xml response. The ID is shown as 110048746230. The xml can easily be parsed in c#. I sued XML Linq below with Load(string FILENAME) method but you can use a string with Parse(string XML) method.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication3
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
string itemID = (string)doc.Descendants().Where(x => x.Name.LocalName == "ItemID").FirstOrDefault();
}
}
}
Related
I have an XML-encoded attribute value. This is actually from a processing instruction. So the original data looks something like this:
<root><?pi key="value" data="<foo attr="bar">Hello world</foo>" ?></root>
I can parse it like this:
using System;
using System.Linq;
using System.Xml.Linq;
public class Program
{
private const string RawData = #"<root><?pi key=""value"" data=""<foo attr="bar">Hello world</foo>"" ?></root>";
public static void Main()
{
XDocument doc = GetXDocumentFromProcessingInstruction();
IEnumerable<XElement> fooElements = doc.Descendants("foo");
// ...
}
private static XProcessingInstruction LoadProcessingInstruction()
{
XDocument doc = XDocument.Parse(rawData);
return doc
.DescendantNodes()
.OfType<XProcessingInstruction>()
.First();
}
private static XDocument GetXDocumentFromProcessingInstruction()
{
XProcessingInstruction processingInstruction = LoadProcessingInstruction();
// QUESTION:
// Can there ever be a situation where HtmlDecode wouldn't decode the XML correctly?
string decodedXml = WebUtility.HtmlDecode(processingInstruction.Data);
// This works well, but it contains the attributes of the processing
// instruction as text.
string dummyXml = $"<dummy>{xml}</dummy>";
return XDocument.Parse(dummyXml);
}
This works absolutely fine, as far as I can tell.
But I am wondering if there might be some edge cases where it may fail, because of differences in how data would be encoded in XML vs. HTML.
Anybody have some more insight?
Edit:
Sorry, I made some incorrect assumptions about XProcessingInstruction.Data, but the code above was still working fine, so my question stands.
I have nonetheless rewritten my code and wrapped everything in an XElement, which (of course) removed the issue altogether:
private static XDocument GetXDocumentFromProcessingInstruction2()
{
XProcessingInstruction processingInstruction = LoadProcessingInstruction();
string encodedXml = string.Format("<dummy {0} />", processingInstruction.Data);
XElement element = XElement.Parse(encodedXml);
string parsedXml = element.Attribute("data").Value;
return XDocument.Parse(parsedXml);
}
So this does exactly what I need. But since WebUtility.HtmlDecode worked sufficiently well, I would still like to know if there could have been a situation where the first approach could have failed.
Removing the question marks and adding a forward slash at end of your input I got this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string input = "<pi data=\"<foo attr="bar">Hello world</foo>\" />";
XElement pi = XElement.Parse(input);
string data = (string)pi.Attribute("data");
XElement foo = XElement.Parse(data);
string attr = (string)foo.Attribute("attr");
string innertext = (string)foo;
}
}
}
What do you think the correct XPath is to pull the dublin core identifier below?
I added a namespace manager with these entries:
// Add the namespace.
XmlNamespaceManager nsmgr = new XmlNamespaceManager(m_xml.NameTable);
nsmgr.AddNamespace("mets", "http://www.loc.gov/METS/");
nsmgr.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");
nsmgr.AddNamespace("dcterms", "http://purl.org/dc/terms/");
And I have tried 15 or so different XPath iterations including the ones below. When I do not get an error, the result is null.
//xml_uuid = m_xml.SelectSingleNode("/mets:mets/mets:dmdSec/mets:mdWrap/mets:xmlData/dcterms:dublincore/dc:identifier").Value;
xml_uuid = m_xml.SelectSingleNode("//dc:identifier",nsmgr).Value;
Here is the xml I am working with:
<mets:mets xmlns:mets="http://www.loc.gov/METS/" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.loc.gov/METS/ http://www.loc.gov/standards/mets/version18/mets.xsd">
<mets:metsHdr CREATEDATE="2017-03-08T20:13:27" />
<mets:dmdSec ID="dmdSec_1">
<mets:mdWrap MDTYPE="DC">
<mets:xmlData>
<dcterms:dublincore xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xsi:schemaLocation="http://purl.org/dc/terms/ http://dublincore.org/schemas/xmls/qdc/2008/02/11/dcterms.xsd">
<dc:identifier>F2015.5</dc:identifier>
</dcterms:dublincore>
</mets:xmlData>
</mets:mdWrap>
</mets:dmdSec>
etc...
I am trying to assign the dc:identifier - F2015.5 in this case - to a string.
With xml linq and ignoring the namespaces :
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication131
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
string results = (string)doc.Descendants().Where(x => x.Name.LocalName == "identifier").FirstOrDefault();
}
}
}
Here is one working answer. I believe the main thing is to use .InnerText and not .Value.
xml_uuid = m_xml.SelectSingleNode("//mets:xmlData[1]/dcterms:dublincore[1]/dc:identifier[1]", nsmgr).InnerText;
In a website we have a code like this
<td>TheNumber</td>
<td>ACode</td>
<td style="width:350px;">A Special String For The Number</td>
I want to create an app that the users write the string and the app get 'TheNumber' from website according to the string.
Can we write an app like this in c#?
You can use HTML Agility Pack to manipulate HTML documents.
sample code
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using HtmlAgilityPack;
public class Program
{
public static void Main()
{
// Load
var doc = new HtmlDocument();
doc.LoadHtml(#"<td>TheNumber</td><td>ACode</td><td style='width:350px;'>A Special String For The Number</td>");
var div = doc.DocumentNode.ChildNodes.Where(t => t.InnerText == "A Special String For The Number").ToList().FirstOrDefault();
// Show info
System.Console.WriteLine(div.PreviousSibling.InnerText);
System.Console.WriteLine(div.PreviousSibling.PreviousSibling.InnerText);
}
}
I have an XML file with IP addresses and their assigned locations/floors that looks like this:
<Locations>
<LOCATION1>
<FLOOR1>
<SECTION1>
<IP>10.10.10.10</IP>
<IP>etc....
</SECTION1>
</FLOOR1>
</LOCATION1>
.....
What I am attempting to do is get query for an IP address and return the names of the parent elements. I am able to query for that IP but I have not had any luck figuring out how to get the parent elements names (i.e. SECTION1, FLOOR1, LOCATION1). Here is the code I am using for querying xml to find the IP, I just have it returning the value at the moment to verify my query was successful:
var query = from t in xmlLocation.Descendants("IP")
where t.Value.Equals(sIP)
select t.Value;
Try this :
XML
<?xml version="1.0" encoding="utf-8" ?>
<Locations>
<LOCATION1>
<FLOOR1>
<SECTION1>
<IP>10.10.10.10</IP>
<IP>20.20.20.20</IP>
</SECTION1>
</FLOOR1>
<FLOOR2>
<SECTION1>
<IP>30.30.30.30</IP>
<IP>40.40.40.40</IP>
</SECTION1>
</FLOOR2>
</LOCATION1>
</Locations >
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
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);
var results = doc.Descendants("LOCATION1").Elements().Select(x => new
{
parent = x.Name.ToString(),
ip = x.Descendants("IP").Select(y => (string)y).ToList()
}).ToList();
}
}
}
The code below gets location, floor, and section
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
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);
var results = doc.Descendants("Locations").Elements().Select(x => x.Elements().Select(y => y.Elements().Select(z => new {
location = x.Name.ToString(),
floor = y.Name.ToString(),
section = z.Name.ToString(),
ip = z.Descendants("IP").Select(i => (string)i).ToList()
})).SelectMany(a => a)).SelectMany(b => b).ToList();
}
}
}
var xDoc = XDocument.Load(filename);
var ip = xDoc.XPathSelectElement("//IP['10.10.10.10']");
var names = ip.Ancestors().Select(a => a.Name.LocalName).ToList();
names will contain SECTION1, FLOOR1, LOCATION1, Locations
If you know those name beforehand you can also use them to select the correct node
var ip = xDoc.XPathSelectElement("//LOCATION1/FLOOR1/SECTION1/IP['10.10.10.10']");
or
var section = xDoc.XPathSelectElement("//LOCATION1/FLOOR1/SECTION1[IP['10.10.10.10']]");
I'm trying to understand how the Swi-cs-pl library works by doing my own little program, but I cannot printout any result from a query based on the example from SWI-Prolog web.
I have this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SbsSW.SwiPlCs;
namespace HolaMundo
{
class Program
{
static void Main(string[] args)
{
if (!PlEngine.IsInitialized)
{
String[] param = { "-q" }; // suppressing informational and banner messages
PlEngine.Initialize(param);
PlQuery.PlCall("assert(father(hader, nevar))");
PlQuery.PlCall("assert(father(hader, sergio))");
PlQuery.PlCall("assert(brother(nevar, sergio):- father(hader, nevar), father(hader, sergio))");
//How do I write out in a Console the answer for a query like:
// brother(nevar, sergio)
PlEngine.PlCleanup();
}
}
}
}
As I said on my code, I just want to query something basic like: brother(nevar, sergio). and get any answer from Console like true or whatever.
Can anyone help me?