I am still getting warmed up with NetSuite and have come across an issue I can't crack.
I am building a C# function to pull information from an XML soap response from NetSuite. This XML is the result of a saved search call which contains a section that is a join titled customSearchJoin that I am unsure how to access. Below I will show the XML section and my attempts to access it in the C# function.
I am attempting to access the field with the value of 091736418
XML segment:
<platformCore:searchRow xsi:type="tranSales:TransactionSearchRow" xmlns:tranSales="urn:sales_2014_1.transactions.webservices.netsuite.com">
<tranSales:basic xmlns:platformCommon="urn:common_2014_1.platform.webservices.netsuite.com">
<platformCommon:dateCreated>
<platformCore:searchValue>2015-12-17T08:43:00.000-08:00</platformCore:searchValue>
</platformCommon:dateCreated>
<platformCommon:entity>
<platformCore:searchValue internalId="615"/>
</platformCommon:entity>
</tranSales:basic>
<tranSales:customerMainJoin xmlns:platformCommon="urn:common_2014_1.platform.webservices.netsuite.com">
<platformCommon:altName>
<platformCore:searchValue>Some Specific Customer</platformCore:searchValue>
</platformCommon:altName>
</tranSales:customerMainJoin>
<tranSales:itemJoin xmlns:platformCommon="urn:common_2014_1.platform.webservices.netsuite.com">
<platformCommon:itemId>
<platformCore:searchValue>Some Product</platformCore:searchValue>
</platformCommon:itemId>
</tranSales:itemJoin>
<tranSales:customSearchJoin xmlns:platformCommon="urn:common_2014_1.platform.webservices.netsuite.com">
<platformCommon:customizationRef internalId="167" scriptId="custrecord_itmfulfillmentid"/>
<platformCommon:searchRowBasic xsi:type="platformCommon:CustomRecordSearchRowBasic">
<platformCommon:recType internalId="25"/>
<platformCommon:customFieldList>
<platformCore:customField xsi:type="platformCore:SearchColumnStringCustomField" scriptId="custrecord_ssccbarcode" internalId="169">
<platformCore:searchValue>091736418</platformCore:searchValue>
</platformCore:customField>
</platformCommon:customFieldList>
</platformCommon:searchRowBasic>
</tranSales:customSearchJoin>
</platformCore:searchRow>
C# function:
private void testCustomJoinSearch() {
TransactionSearchAdvanced transSearchAdv = new TransactionSearchAdvanced
{
savedSearchScriptId = "customsearch_savedSearchID"
};
SearchResult searchResult = _service.search(transSearchAdv);
if (searchResult.status.isSuccess)
{
Console.WriteLine("Search Success");
foreach (TransactionSearchRow transSearchRow in searchResult.searchRowList)
{
// declare vars
string transInternalID = "";
string ssccBarcode = "";
//normal field
transInternalID = transSearchRow.basic. internalId[0].searchValue.internalId.ToString();
//joined and custom field ? Not sure this loop is correct
foreach (CustomSearchRowBasic customBasicSearchRow in transSearchRow.customSearchJoin)
{
// ????
};
Console.WriteLine("transInternalID {0}", transInternalID);
Console.WriteLine("ssccBarcode {0}", ssccBarcode);
} // end main search row
}
else
{
Console.WriteLine("Search Failure");
Console.WriteLine(searchResult.status.statusDetail);
}
}
Try xml linq. I fixed your xml that was missing some namespace definitions so I added root and then closed the end tags platformCore:searchRow and root.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication70
{
class Program
{
static void Main(string[] args)
{
string xml =
"<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
"<Root xmlns:platformCore=\"abc\" xmlns:xsi=\"def\">" +
"<platformCore:searchRow xsi:type=\"tranSales:TransactionSearchRow\" xmlns:tranSales=\"urn:sales_2014_1.transactions.webservices.netsuite.com\">" +
"<tranSales:basic xmlns:platformCommon=\"urn:common_2014_1.platform.webservices.netsuite.com\">" +
"<platformCommon:dateCreated>" +
"<platformCore:searchValue>2015-12-17T08:43:00.000-08:00</platformCore:searchValue>" +
"</platformCommon:dateCreated>" +
"<platformCommon:entity>" +
"<platformCore:searchValue internalId=\"615\"/>" +
"</platformCommon:entity>" +
"</tranSales:basic>" +
"<tranSales:customerMainJoin xmlns:platformCommon=\"urn:common_2014_1.platform.webservices.netsuite.com\">" +
"<platformCommon:altName>" +
"<platformCore:searchValue>Some Specific Customer</platformCore:searchValue>" +
"</platformCommon:altName>" +
"</tranSales:customerMainJoin>" +
"<tranSales:itemJoin xmlns:platformCommon=\"urn:common_2014_1.platform.webservices.netsuite.com\">" +
"<platformCommon:itemId>" +
"<platformCore:searchValue>Some Product</platformCore:searchValue>" +
"</platformCommon:itemId>" +
"</tranSales:itemJoin>" +
"<tranSales:customSearchJoin xmlns:platformCommon=\"urn:common_2014_1.platform.webservices.netsuite.com\">" +
"<platformCommon:customizationRef internalId=\"167\" scriptId=\"custrecord_itmfulfillmentid\"/>" +
"<platformCommon:searchRowBasic xsi:type=\"platformCommon:CustomRecordSearchRowBasic\">" +
"<platformCommon:recType internalId=\"25\"/>" +
"<platformCommon:customFieldList>" +
"<platformCore:customField xsi:type=\"platformCore:SearchColumnStringCustomField\" scriptId=\"custrecord_ssccbarcode\" internalId=\"169\">" +
"<platformCore:searchValue>091736418</platformCore:searchValue>" +
"</platformCore:customField>" +
"</platformCommon:customFieldList>" +
"</platformCommon:searchRowBasic>" +
"</tranSales:customSearchJoin>" +
"</platformCore:searchRow>" +
"</Root>";
XDocument doc = XDocument.Parse(xml);
string searchvalue = doc.Descendants().Where(x => x.Name.LocalName == "customSearchJoin")
.Descendants().Where(y => y.Name.LocalName == "searchValue").Select(z => (string)z).FirstOrDefault();
}
}
}
I believe it is something like this:
foreach (CustomSearchRowBasic customBasicSearchRow in transSearchRow.customSearchJoin) {
// ????
CustomRecordSearchRowBasic itmfulfillmentidRecord = customBasicSearchRow.searchRowBasic as CustomRecordSearchRowBasic;
foreach(SearchColumnCustomField customField in itmfulfillmentidRecord.customFieldList) {
if (customField.scriptId == "custrecord_ssccbarcode") {
SearchColumnStringCustomField stringField = customField as SearchColumnStringCustomField;
string itmfulfillmentid = stringField.searchValue;
}
}
}
Related
I have an XML string which looks like below
<?xml version="1.0" encoding="Windows-1252"?><Product><ID>0701161476416</ID><UNIQUE_ID>test26051602</UNIQUE_ID><STATUS>DONE</STATUS></Product>
It is know that my XML string will always has a single node ans so I do not want to look, instead I would like to get Unique_ID and Status inner values without looping.
May I know a better way to do it and I do have the below code which actually loops through each node
XmlDocument xm = new XmlDocument();
xm.LoadXml(XML_STRING);
XmlNodeList xnList = xm.SelectNodes("/Product/Product");
foreach (XmlNode xn in xnList)
{
string uniqueID = xn["UNIQUE_ID"].InnerText;
string status = xn["STATUS"].InnerText;
}
There is SelectSingleNode() which you can use for this purpose :
XmlNode product = xm.SelectSingleNode("/Product/Product");
string uniqueID = product["UNIQUE_ID"].InnerText;
string status = product["STATUS"].InnerText;
Or, if Product is the root element, then you can access it from DocumentElement property of the XmlDocument :
XmlNode product = xm.DocumentElement;
string uniqueID = product["UNIQUE_ID"].InnerText;
string status = product["STATUS"].InnerText;
If you have more than one product try xml linq
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 xml =
"<?xml version=\"1.0\" encoding=\"Windows-1252\"?>" +
"<Products>" +
"<Product>" +
"<ID>0701161476416</ID>" +
"<UNIQUE_ID>test26051603</UNIQUE_ID>" +
"<STATUS>DONE</STATUS>" +
"</Product>" +
"<Product>" +
"<ID>0701161476417</ID>" +
"<UNIQUE_ID>test26051604</UNIQUE_ID>" +
"<STATUS>DONE</STATUS>" +
"</Product>" +
"<Product>" +
"<ID>0701161476418</ID>" +
"<UNIQUE_ID>test26051605</UNIQUE_ID>" +
"<STATUS>DONE</STATUS>" +
"</Product>" +
"</Products>";
XDocument doc = XDocument.Parse(xml);
var results = doc.Descendants("Product").Select(x => new
{
id = (long)x.Element("ID"),
uniqueID = (string)x.Element("UNIQUE_ID"),
status = (string)x.Element("STATUS")
}).ToList();
}
}
}
This could work, but I guess there is a better solution:
XDocument xm = XDocument.Parse(XML_STRING);
var product = xm.Element("Product").Element("Product");
string uniqueID = product.Element("UNIQUE_ID").Value;
string status = product.Element("STATUS").Value;
Your SelectNodes line seemed wrong for the sample Xml.
XmlNodeList xnList = xm.SelectNodes("/Product");
if (xnList.Count > 0)
{
string uniqueID = xnList[0]["UNIQUE_ID"].InnerText;
string status = xnList[0]["STATUS"].InnerText;
}
I am trying to user Linq to XML to pull out a value from some xml based on another value..
Here is my xml
<Lead>
<ID>1189226</ID>
<Client>
<ID>8445254</ID>
<Name>City of Lincoln Council</Name>
</Client>
<Contact>
<ID>5747449</ID>
<Name>Fred Bloggs</Name>
</Contact>
<Owner>
<ID>36612</ID>
<Name>Joe Bloggs</Name>
</Owner>
<CustomFields>
<CustomField>
<ID>31961</ID>
<Name>Scotsm_A_n_Authority_Good</Name>
<Boolean>true</Boolean>
</CustomField>
<CustomField>
<ID>31963</ID>
<Name>Scotsma_N_Need Not Want</Name>
<Boolean>false</Boolean>
</CustomField>
</CustomFields>
So, for example, I want to try and get the value of <Boolean> in the <CustomField> where <Name> equals "Scotsm_A_n_Authority_Good" which should be "true"
I have tried the following:
var xmlAnswers = xe.Element("CustomFields").Element("CustomField").Element("Scotsm_A_n_Authority_Good");
But I get an error saying that:
The ' ' character, hexadecimal value 0x20, cannot be included in a name...
Can anyone help please?
You're looking for the wrong thing at the moment. You should be looking for a CustomField element with a Name element with the specified value:
string target = "Scotsm_A_n_Authority_Good"; // Or whatever
var xmlAnswers = xe.Element("CustomFields")
.Elements("CustomField")
.Where(cf => (string) cf.Element("Name") == target);
This will give you all the matching CustomField elements. You can then get the Boolean value with something like:
foreach (var answer in xmlAnswers)
{
int id = (int) answer.Element("ID");
bool value = (bool) answer.Element("Boolean");
// Use them...
}
(You could extract the ID and value in the LINQ query of course...)
Here is an xml linq solution
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication93
{
class Program
{
static void Main(string[] args)
{
string xml =
"<Lead>" +
"<ID>1189226</ID>" +
"<Client>" +
"<ID>8445254</ID>" +
"<Name>City of Lincoln Council</Name>" +
"</Client>" +
"<Contact>" +
"<ID>5747449</ID>" +
"<Name>Fred Bloggs</Name>" +
"</Contact>" +
"<Owner>" +
"<ID>36612</ID>" +
"<Name>Joe Bloggs</Name>" +
"</Owner>" +
"<CustomFields>" +
"<CustomField>" +
"<ID>31961</ID>" +
"<Name>Scotsm_A_n_Authority_Good</Name>" +
"<Boolean>true</Boolean>" +
"</CustomField>" +
"<CustomField>" +
"<ID>31963</ID>" +
"<Name>Scotsma_N_Need Not Want</Name>" +
"<Boolean>false</Boolean>" +
"</CustomField>" +
"</CustomFields>" +
"</Lead>";
XDocument doc = XDocument.Parse(xml);
//to get all customField
var results = doc.Descendants("CustomField").Select(x => new
{
id = (int)x.Element("ID"),
name = (string)x.Element("Name"),
boolean = (Boolean)x.Element("Boolean")
}).ToList();
//to get specific
XElement Scotsm_A_n_Authority_Good = doc.Descendants("CustomField")
.Where(x => ((string)x.Element("Name") == "Scotsm_A_n_Authority_Good") && (Boolean)(x.Element("Boolean"))).FirstOrDefault();
}
}
}
I need to create a WebService that takes the name of a city as input and returns Location, Country and Weather information of the City. The thing is that the ID, Location, Country is in one XML file and all weather details in another.
<City>
<ID>city1</ID>
<Grid_ref>NG 895608</Grid_ref>
<Name>Berlin</Name>
<Country>Germany</Country>
</City>
<cityWeather>
<ID>city1</ID>
<temperature>20</temperature>
<wind>2</wind>
</cityWeather>
Using c# is it possible to merge everything into 1 file using ID or is there some other way I can do it? I would then search the XML file once, as having 2 different files mixes me up.
You can use DataSet. I suppose you have two XML files. CityWeather.xml et City.xml, you can make this
try
{
XmlTextReader xmlreader1 = new XmlTextReader("C:\\Books1.xml");
XmlTextReader xmlreader2 = new XmlTextReader("C:\\Books2.xml");
DataSet ds = new DataSet();
ds.ReadXml(xmlreader1);
DataSet ds2 = new DataSet();
ds2.ReadXml(xmlreader2);
ds.Merge(ds2);
ds.WriteXml("C:\\Books.xml");
Console.WriteLine("Completed merging XML documents");
}
catch (System.Exception ex)
{
Console.Write(ex.Message);
}
Console.Read();
You can make any changes that meet your need
hope it helps
Use add
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 cityXML =
"<Root>" +
"<City>" +
"<ID>city1</ID>" +
"<Grid_ref>NG 895608</Grid_ref>" +
"<Name>Berlin</Name>" +
"<Country>Germany</Country>" +
"</City>" +
"<City>" +
"<ID>city2</ID>" +
"<Grid_ref>F 5608</Grid_ref>" +
"<Name>Paris</Name>" +
"<Country>France</Country>" +
"</City>" +
"<City>" +
"<ID>city3</ID>" +
"<Grid_ref>RR 608</Grid_ref>" +
"<Name>Rome</Name>" +
"<Country>Italy</Country>" +
"</City>" +
"</Root>";
XElement cities = XElement.Parse(cityXML);
string weatherXML =
"<Root>" +
"<cityWeather>" +
"<ID>city1</ID>" +
"<temperature>20</temperature>" +
"<wind>2</wind>" +
"</cityWeather>" +
"<cityWeather>" +
"<ID>city2</ID>" +
"<temperature>30</temperature>" +
"<wind>3</wind>" +
"</cityWeather>" +
"<cityWeather>" +
"<ID>city3</ID>" +
"<temperature>40</temperature>" +
"<wind>4</wind>" +
"</cityWeather>" +
"</Root>";
XElement weather = XElement.Parse(weatherXML);
List<XElement> cityList = cities.Descendants("City").ToList();
foreach(XElement city in cityList)
{
XElement matchedCity = weather.Descendants("cityWeather").Where(x =>
x.Element("ID").Value == city.Element("ID").Value).FirstOrDefault();
if(matchedCity != null) city.Add(matchedCity);
}
}
}
}
I have used the following code before but my xml is different this time:
protected string ReturnXmlValue(XmlDocument myXDoc, string field)
{
var retval = string.Empty;
try
{
var node = myXDoc.GetElementsByTagName(field);
if (node.Count > 0)
{
var xmlNode = node.Item(0);
if (xmlNode != null)
{
retval = xmlNode.InnerText;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
throw;
}
return retval;
}
Here is an example of my xml file dummied down a bit:
<RichDBDS>
<TrxDetailCard>
<TRX_HD_Key>18683435</TRX_HD_Key>
<Date_DT>2015-10-22T21:32:00.233+00:00</Date_DT>
<TRX_Card_Key>15263569</TRX_Card_Key>
<Total_Amt_MN>22.0000</Total_Amt_MN>
<Result_CH>0 </Result_CH>
<Result_Txt_VC>APPROVED</Result_Txt_VC>
<Approval_Code_CH>0943253</Approval_Code_CH>
</TrxDetailCard>
<TrxDetailCard>
<TRX_HD_Key>18683825</TRX_HD_Key>
<Date_DT>2015-10-23T21:32:00.233+00:00</Date_DT>
<TRX_Card_Key>15263569</TRX_Card_Key>
<Total_Amt_MN>32.0000</Total_Amt_MN>
<Result_CH>0 </Result_CH>
<Result_Txt_VC>APPROVED</Result_Txt_VC>
<Approval_Code_CH>093389</Approval_Code_CH>
</TrxDetailCard>
</RichDBDS>
I've not worked with xml much so I'm not sure how to search this for a specific amount. I can have several TrxDetailCards. I know how to get amount when I only have one TrxDetailCard but I need to return the TrxDetailCard for the hits on the amount that I need.
So if I am looking for the TrxDetailCard that is 32.00, I need the method to return:
<TrxDetailCard>
<TRX_HD_Key>18683825</TRX_HD_Key>
<Date_DT>2015-10-23T21:32:00.233+00:00</Date_DT>
<TRX_Card_Key>15263569</TRX_Card_Key>
<Total_Amt_MN>32.0000</Total_Amt_MN>
<Result_CH>0 </Result_CH>
<Result_Txt_VC>APPROVED</Result_Txt_VC>
<Approval_Code_CH>093389</Approval_Code_CH>
</TrxDetailCard>
How would I go about doing this?
Try 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 xml =
"<RichDBDS>" +
"<TrxDetailCard>" +
"<TRX_HD_Key>18683435</TRX_HD_Key>" +
"<Date_DT>2015-10-22T21:32:00.233+00:00</Date_DT>" +
"<TRX_Card_Key>15263569</TRX_Card_Key>" +
"<Total_Amt_MN>22.0000</Total_Amt_MN>" +
"<Result_CH>0 </Result_CH>" +
"<Result_Txt_VC>APPROVED</Result_Txt_VC>" +
"<Approval_Code_CH>0943253</Approval_Code_CH>" +
"</TrxDetailCard>" +
"<TrxDetailCard>" +
"<TRX_HD_Key>18683825</TRX_HD_Key>" +
"<Date_DT>2015-10-23T21:32:00.233+00:00</Date_DT>" +
"<TRX_Card_Key>15263569</TRX_Card_Key>" +
"<Total_Amt_MN>32.0000</Total_Amt_MN>" +
"<Result_CH>0 </Result_CH>" +
"<Result_Txt_VC>APPROVED</Result_Txt_VC>" +
"<Approval_Code_CH>093389</Approval_Code_CH>" +
"</TrxDetailCard>" +
"</RichDBDS>";
XElement richDBDS = XElement.Parse(xml);
XElement results = richDBDS.Elements("TrxDetailCard").Where(x => (decimal)x.Element("Total_Amt_MN") == (decimal)32.0000).FirstOrDefault();
}
}
}
You can use Linq-to-Xml
var str = File.ReadAllText(#"C:\YourDirectory\sample.xml");
XDocument xDoc = XDocument.Parse(str);
var trxNodes = xDoc.Descendants("TrxDetailCard");
var node = trxNodes.First(n => double.Parse(n.Element("Total_Amt_MN").Value) == 32.00);
I would like to pass an List response as an xml document and want to load it as xml.
Currently i am performing it using this code :
List<string> BugWSResponseList1 = new List<string>();
BugWSResponseList1 = CreateBugs(FilePath_EXPRESS_API_BugAdd_CreateBugs_DataFile, newValue);
XmlDocument xd = new XmlDocument();
//string s = string.Join(",", BugWSResponseList2);
//xd.LoadXml(s);
string s = "<Bugs>" + string.Join(",", BugWSResponseList1) + "</Bugs>";
xd.LoadXml(s);
//Dictionary Creation for Benchmark API
XmlDocument ResponseNode_bugs = null;
ResponseNode_bugs = new DrWatsonCore().LoadXMLDocument(s);
I would like to avoid to convert the List<string> response to string first and then loading it as xml.
Please suggest how can i achieve that.
Please find the detail of my LoadXMLDocument function as follows:
public XmlDocument LoadXMLDocument(string XMLData)
{
XmlDocument doc = new XmlDocument();
try
{
doc.LoadXml(XMLData);
}
catch (Exception ex)
{
Logger.Write("\n\n" + DateTime.Now + " : " + "LoadXMLDocument : SEVERE ERROR : Failed to create XMLDocument from given string : \n\n");
Logger.Write("\n\n" + DateTime.Now + " : " + "Exception : : \n\n" + ex.Message);
Logger.Write("\n\n" + DateTime.Now + " : " + "Given String : : \n\n");
if (DrWatsonHome.doVerboseLogging)
{
Logger.Write(XMLData);
}
else
{
string Response = XMLData;
Logger.Write("\n\n" + DateTime.Now + " : " + "Response Length : " + Response.Length + " characters.");
int ResponseLengthToPrint = DrWatsonHome.VerboseLogging_Length_Chars < Response.Length ? DrWatsonHome.VerboseLogging_Length_Chars : Response.Length;
Logger.Write("\n\n" + DateTime.Now + " : " + "Response : " + Response.Substring(0, ResponseLengthToPrint) + " ...");
}
}
finally
{
}
return doc;
}
Can i achieve it after making some modifications in it.Please suggest.
I am using List now and my function is as follows:
List<XElement> Sairam(string FilePath, string Timestamp)
{
List<XElement> WSResponse = new List<XElement>();
XmlDocument XDoc = new DrWatsonCore().LoadXMLFromFile(FilePath);
XmlNode BugListNode = XDoc.SelectSingleNode("/DrWatson/Bugs");
List<Bug> BugList = new DrWatsonCore().GetBugList(BugListNode);
List<Thread> BugFtecherThreadList = new List<Thread>();
foreach (Bug bug in BugList)
{
bug.FoundInBuild = Timestamp;
string URL = new DrWatsonCore().CreateWXURL(EXPRESS_API_METHOD.bug_add, bug, REST_EndPoint, AppGUID, new WatsonUser(Username_Watson, Password_Watson));
string Response = string.Empty;
object URLobj = (object) URL;
Response = CreateBug(URLobj);
// WSResponse.Add(Response);
}
return WSResponse;
}
I would like to get the WSResponse as the final response of teh program but i am unable to fetch it. Please suggest so how can i get the response of this program from this function.