XML file is overwritten in each run - c#

I am trying to write some data into XML file.
Actually, I can do that, but in each run, the XML file is overwritten while I want it to add another row.
This is what I have done so far:
public static void StoreCustomerIntoXML(string Id)
{
string pth = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
string actualPath = pth.Substring(0, pth.LastIndexOf("bin"));
string projectPath = new Uri(actualPath).LocalPath;
string reportPath = projectPath + "Customers\\CustomersListCreated.xml";
XmlDocument xmlDoc = new XmlDocument();
XmlNode rootNode = xmlDoc.CreateElement("Customers");
xmlDoc.AppendChild(rootNode);
XmlNode userNode = xmlDoc.CreateElement("Id");
userNode.InnerText = Id;
rootNode.AppendChild(userNode);
xmlDoc.Save(reportPath);
}
So calling the method for the first time will include Id = 1234
and the second run will include Id = 6543
The XML file will always include the Id of last run and only this Id.

Try this
public static void StoreCustomerIntoXML(string Id)
{
string pth = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
string actualPath = pth.Substring(0, pth.LastIndexOf("bin"));
string projectPath = new Uri(actualPath).LocalPath;
string reportPath = projectPath + "CustomersListCreated.xml";
XmlDocument xmlDoc = new XmlDocument();
if (File.Exists(reportPath))
{
xmlDoc.Load(reportPath);
XmlNode rootNode = xmlDoc.DocumentElement;
xmlDoc.AppendChild(rootNode);
XmlElement elem = xmlDoc.CreateElement("Id");
elem.InnerText = Id;
rootNode.AppendChild(elem);
}
else
{
XmlNode rootNode = xmlDoc.CreateElement("Customers");
xmlDoc.AppendChild(rootNode);
XmlNode userNode = xmlDoc.CreateElement("Id");
userNode.InnerText = Id;
rootNode.AppendChild(userNode);
}
xmlDoc.Save(reportPath);
}

You could check if the file exists and load it using the method Load, instead of creating a new one every time you call your method.
public static void StoreCustomerIntoXML(string Id)
{
string pth = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
string actualPath = pth.Substring(0, pth.LastIndexOf("bin"));
string projectPath = new Uri(actualPath).LocalPath;
string reportPath = projectPath + "Customers\\CustomersListCreated.xml";
XmlDocument xmlDoc;
if (File.Exists(reportPath))
xmlDoc = XDocument.Load(reportPath);
else
xmlDoc = new XmlDocument();
XmlNode rootNode = xmlDoc.CreateElement("Customers");
xmlDoc.AppendChild(rootNode);
XmlNode userNode = xmlDoc.CreateElement("Id");
userNode.InnerText = Id;
rootNode.AppendChild(userNode);
xmlDoc.Save(reportPath);
}

public static void StoreCustomerIntoXML(string Id)
{
string pth = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
string actualPath = pth.Substring(0, pth.LastIndexOf("bin"));
string projectPath = new Uri(actualPath).LocalPath;
string reportPath = projectPath + "Customers\\CustomersListCreated.xml";
XmlDocument xmlDoc = new XmlDocument();
XmlNode rootNode;
if (File.Exists(reportPath))
{
xmlDoc.Load(reportPath);
rootNode = xmlDoc.DocumentElement;
}
else
{
rootNode = xmlDoc.CreateElement("Customers");
xmlDoc.AppendChild(rootNode);
}
XmlNode userNode = xmlDoc.CreateElement("Id");
userNode.InnerText = Id;
rootNode.AppendChild(userNode);
xmlDoc.Save(reportPath);
}

Related

Cannot change XmlDocument value?

I have a simple function which will simply change and read the value.
void ParseXml(string XmlFile)
{
string totalval = "";
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(new StringReader(XmlFile));
string xmlPathPattern = "//name";
XmlNodeList mynodelist = xmldoc.SelectNodes(xmlPathPattern);
foreach (XmlNode node in mynodelist)
{
XmlNode name = node.FirstChild;
name.Value = "asd";//here I am trying to change value
totalval = totalval + "Name=" + name.OuterXml + "\n";
}
xmldoc.Save(XmlFile);
print(totalval);
}
This is my .xml file.
<name>John</name>
I can successfully read the value but it is not changing the value from .xml file.After running the program it must be like this
<name>asd</name> .
Where is my mistake ?
Obviously, the XMLFile is not a file path, it is xml string. So, you should define a valid path to save it.
xmldoc.Save("samplefile.xml");
or if you want to set the XmlFile variable with modified xml;
XmlFile = xmldoc.OuterXml;
Complete codes look like;
void ParseXml(string XmlFile)
{
string totalval = "";
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(new StringReader(XmlFile));
string xmlPathPattern = "//name";
XmlNodeList mynodelist = xmldoc.SelectNodes(xmlPathPattern);
foreach (XmlNode node in mynodelist)
{
XmlNode name = node.FirstChild;
name.Value = "asd";//here I am trying to change value
totalval = totalval + "Name=" + name.OuterXml + "\n";
}
//XmlFile = xmldoc.OuterXml;
xmldoc.Save("samplefile.xml");
print(totalval);
}
If I'm not wrong - you need to fire disposable to save stream. Easiest way - wrap with using
void ParseXml(string XmlFile)
{
string totalval = "";
using(XmlDocument xmldoc = new XmlDocument())
{
xmldoc.Load(new StringReader(XmlFile));
string xmlPathPattern = "//name";
XmlNodeList mynodelist = xmldoc.SelectNodes(xmlPathPattern);
foreach (XmlNode node in mynodelist)
{
XmlNode name = node.FirstChild;
name.Value = "asd";//here I am trying to change value
totalval = totalval + "Name=" + name.OuterXml + "\n";
}
xmldoc.Save(XmlFile);
print(totalval);
}
}

Encode & Decode string in WPF C#

I am trying to encode & decode strings that are saved to an .ini settings file for a WPF app.
encoding & decoding class
class EncryptDecrypt
{
public static string Base64Encode(string plainText)
{
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
return System.Convert.ToBase64String(plainTextBytes);
}
public static string Base64Decode(string base64EncodedData)
{
var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
}
}
The encoding seems to be saved to the .ini file correctly, but decoding the entry from the .ini file seems to output incorrect string as per below;
encoding from password field works fine (on save_click)
inif.Write("APISettings", "HashKey", EncryptDecrypt.Base64Encode(hash_key.Password.ToString()));
but, reload app then navigating to same field/form the decode is not outputting entry correctly (as it is used to check http status of page)..
hash_key.Password = EncryptDecrypt.Base64Decode(inif.Read("APISettings", "HashKey"));
I can see that is outputting the string as weird wingdings characters.
UPDATE
Resolved by instead of saving to .ini file I saved to xml document, see below;
private void Save_Click(object sender, RoutedEventArgs e)
{
//Settings Data
string appfolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string ApplicationPath = System.IO.Path.Combine(appfolder, "MyApp/Application");
if (!Directory.Exists(ApplicationPath)) Directory.CreateDirectory(ApplicationPath);
//Encryption
if (!System.IO.File.Exists(ApplicationPath + #"\credentials.xml"))
{
XmlDocument doc = new XmlDocument();
//(1) the xml declaration is recommended, but not mandatory
XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
XmlElement root = doc.DocumentElement;
doc.InsertBefore(xmlDeclaration, root);
//(2) string.Empty makes cleaner code
XmlElement elementNode = doc.CreateElement(string.Empty, "Credentials", string.Empty);
doc.AppendChild(elementNode);
//Base URL
XmlElement elementNodeBaseUrl = doc.CreateElement(string.Empty, "BaseUrl", string.Empty);
elementNode.AppendChild(elementNodeBaseUrl);
elementNodeBaseUrl.InnerText = base_url.Text.ToString();
XmlElement elementNodeAdminPath = doc.CreateElement(string.Empty, "AdminPath", string.Empty);
elementNode.AppendChild(elementNodeAdminPath);
elementNodeAdminPath.InnerText = EncryptDecrypt.Base64Encode(admin_path.Password.ToString());
XmlElement elementNodeAdminName = doc.CreateElement(string.Empty, "AdminName", string.Empty);
elementNode.AppendChild(elementNodeAdminName);
elementNodeAdminName.InnerText = EncryptDecrypt.Base64Encode(admin_name.Password.ToString());
XmlElement elementNodeAdminPassword = doc.CreateElement(string.Empty, "AdminPassword", string.Empty);
elementNode.AppendChild(elementNodeAdminPassword);
elementNodeAdminPassword.InnerText = EncryptDecrypt.Base64Encode(admin_password.Password.ToString());
XmlElement elementNodeHashKey = doc.CreateElement(string.Empty, "HashKey", string.Empty);
elementNode.AppendChild(elementNodeHashKey);
elementNodeHashKey.InnerText = EncryptDecrypt.Base64Encode(hash_key.Password.ToString());
XmlElement elementNodeUpdated = doc.CreateElement(string.Empty, "Updated", string.Empty);
elementNode.AppendChild(elementNodeUpdated);
elementNodeUpdated.InnerText = DateTime.Now.ToString();
doc.Save(ApplicationPath + #"\credentials.xml");
}else{
XmlDocument doc = new XmlDocument();
//(1) the xml declaration is recommended, but not mandatory
XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
XmlElement root = doc.DocumentElement;
doc.InsertBefore(xmlDeclaration, root);
//(2) string.Empty makes cleaner code
XmlElement elementNode = doc.CreateElement(string.Empty, "Credentials", string.Empty);
doc.AppendChild(elementNode);
//Base URL
XmlElement elementNodeBaseUrl = doc.CreateElement(string.Empty, "BaseUrl", string.Empty);
elementNode.AppendChild(elementNodeBaseUrl);
elementNodeBaseUrl.InnerText = base_url.Text.ToString();
XmlElement elementNodeAdminPath = doc.CreateElement(string.Empty, "AdminPath", string.Empty);
elementNode.AppendChild(elementNodeAdminPath);
elementNodeAdminPath.InnerText = EncryptDecrypt.Base64Encode(admin_path.Password.ToString());
XmlElement elementNodeAdminName = doc.CreateElement(string.Empty, "AdminName", string.Empty);
elementNode.AppendChild(elementNodeAdminName);
elementNodeAdminName.InnerText = EncryptDecrypt.Base64Encode(admin_name.Password.ToString());
XmlElement elementNodeAdminPassword = doc.CreateElement(string.Empty, "AdminPassword", string.Empty);
elementNode.AppendChild(elementNodeAdminPassword);
elementNodeAdminPassword.InnerText = EncryptDecrypt.Base64Encode(admin_password.Password.ToString());
XmlElement elementNodeHashKey = doc.CreateElement(string.Empty, "HashKey", string.Empty);
elementNode.AppendChild(elementNodeHashKey);
elementNodeHashKey.InnerText = EncryptDecrypt.Base64Encode(hash_key.Password.ToString());
XmlElement elementNodeUpdated = doc.CreateElement(string.Empty, "Updated", string.Empty);
elementNode.AppendChild(elementNodeUpdated);
elementNodeUpdated.InnerText = DateTime.Now.ToString();
doc.Save(ApplicationPath + #"\credentials.xml");
}
Thread.Sleep(1000);
SaveButton.Visibility = Visibility.Hidden;
onclick_progress.Visibility = Visibility.Visible;
showProgress();
}

Reading xml file with multiple child nodes only returns first node

I have an xml file with a namespace and i can read it properly.It has an outer node,called 'Items' which has multiple child nodes,50 of them.(So 50 child nodes called 'ReceiverPoints').When i check the console,its size is correct i.e 50 but when i check the print out,all the output is a repetition of just the first ReceiverPoint node.
I would like to save each receiver point into the database.According to all the examples i have seen,my implementation seems fine.But it gives me wrong results.Could someone help me see what i'm missing?This is the xml file
public List<ReceiverPoint> ReadFile()
{
receiverList = new List<ReceiverPoint> ();
Console.WriteLine ("Now in read file method :" + fileLocation);
xmldoc = new XmlDocument ();
xmldoc.Load (fileLocation);
XmlNamespaceManager nameSpaceManager = new XmlNamespaceManager (xmldoc.NameTable);
//nameSpaceManager.AddNamespace ("ns", "http://schemas.datacontract.org/2004/07/AristotleService.Models");
nameSpaceManager.AddNamespace ("ns", "http://schemas.datacontract.org/2004/07/GTI.Aristotle.Web.Api.Models");
XmlElement rootElement = xmldoc.DocumentElement;
XmlNodeList nodeList = rootElement.SelectNodes("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint", nameSpaceManager);
ReceiverPoint receiverPoint = new ReceiverPoint ();
foreach(XmlNode childNode in nodeList)
{
receiverPoint.CloseDate = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:CloseDate", nameSpaceManager).InnerText;
receiverPoint.CreateDate = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:CreateDate", nameSpaceManager).InnerText;
receiverPoint.CreateWho = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:CreateWho", nameSpaceManager).InnerText;
receiverPoint.Easting = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:Easting", nameSpaceManager).InnerText;
receiverPoint.Elevation = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:Elevation", nameSpaceManager).InnerText;
receiverPoint.IsDeployed = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:IsDeployed", nameSpaceManager).InnerText;
receiverPoint.IsManual = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:IsManual", nameSpaceManager).InnerText;
receiverPoint.LastModifyDate = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:LastModifyDate", nameSpaceManager).InnerText;
receiverPoint.Latitude = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:LatitudeWGS84", nameSpaceManager).InnerText;
receiverPoint.Line = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:Line", nameSpaceManager).InnerText;
receiverPoint.Longitude = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:LongitudeWGS84", nameSpaceManager).InnerText;
receiverPoint.ReceiverType = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:ReceiverType", nameSpaceManager).InnerText;
receiverPoint.Station = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:Station", nameSpaceManager).InnerText;
//Get all the values stored in the receiver point object
string station = receiverPoint.Station;
string line = receiverPoint.Line;
string elevation = receiverPoint.Elevation;
string latitude = receiverPoint.Latitude;
string longitude = receiverPoint.Longitude;
string isDeployed = receiverPoint.IsDeployed;
string easting = receiverPoint.Easting;
string receiverType = receiverPoint.ReceiverType;
string closeDate = receiverPoint.CloseDate;
string createDate = receiverPoint.CreateDate;
string createWho = receiverPoint.CreateWho;
string lastModifyDate = receiverPoint.LastModifyDate;
Console.WriteLine ("String lat : " + latitude);
Console.WriteLine ("String lon : " + longitude);
Console.WriteLine ("String create date : " + createDate);
Console.WriteLine ("String create who : " + createWho);
//Save the data to the db
saveDataToDatabase (station,line,elevation,latitude,longitude,isDeployed,easting,receiverType,closeDate,createDate,createWho,lastModifyDate);
}
receiverList.Add (receiverPoint);
return receiverList;
}
This code work for me:
public List<ReceiverPoint> ReadFile()
{
var receiverList = new List<ReceiverPoint>();
Console.WriteLine("Now in read file method :" + "");
var xmldoc = new XmlDocument();
xmldoc.Load(#"D:\users\..\Downloads\ReceiverPoints.xml");
XmlNamespaceManager nameSpaceManager = new XmlNamespaceManager(xmldoc.NameTable);
//nameSpaceManager.AddNamespace ("ns", "http://schemas.datacontract.org/2004/07/AristotleService.Models");
nameSpaceManager.AddNamespace("ns", "http://schemas.datacontract.org/2004/07/GTI.Aristotle.Web.Api.Models");
XmlElement rootElement = xmldoc.DocumentElement;
XmlNodeList nodeList = rootElement.SelectNodes("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint", nameSpaceManager);
foreach (XmlNode childNode in nodeList)
{
ReceiverPoint receiverPoint = new ReceiverPoint();
receiverPoint.CloseDate = childNode.SelectSingleNode("ns:CloseDate", nameSpaceManager).InnerText;
receiverPoint.CreateDate = childNode.SelectSingleNode("ns:CreateDate", nameSpaceManager).InnerText;
receiverPoint.CreateWho = childNode.SelectSingleNode("ns:CreateWho", nameSpaceManager).InnerText;
receiverPoint.Easting = childNode.SelectSingleNode("ns:Easting", nameSpaceManager).InnerText;
receiverPoint.Elevation = childNode.SelectSingleNode("ns:Elevation", nameSpaceManager).InnerText;
receiverPoint.IsDeployed = childNode.SelectSingleNode("ns:IsDeployed", nameSpaceManager).InnerText;
receiverPoint.IsManual = childNode.SelectSingleNode("ns:IsManual", nameSpaceManager).InnerText;
receiverPoint.LastModifyDate = childNode.SelectSingleNode("ns:LastModifyDate", nameSpaceManager).InnerText;
receiverPoint.Latitude = childNode.SelectSingleNode("ns:LatitudeWGS84", nameSpaceManager).InnerText;
receiverPoint.Line = childNode.SelectSingleNode("ns:Line", nameSpaceManager).InnerText;
receiverPoint.Longitude = childNode.SelectSingleNode("ns:LongitudeWGS84", nameSpaceManager).InnerText;
receiverPoint.ReceiverType = childNode.SelectSingleNode("ns:ReceiverType", nameSpaceManager).InnerText;
receiverPoint.Station = childNode.SelectSingleNode("ns:Station", nameSpaceManager).InnerText;
//Get all the values stored in the receiver point object
string station = receiverPoint.Station;
string line = receiverPoint.Line;
string elevation = receiverPoint.Elevation;
string latitude = receiverPoint.Latitude;
string longitude = receiverPoint.Longitude;
string isDeployed = receiverPoint.IsDeployed;
string easting = receiverPoint.Easting;
string receiverType = receiverPoint.ReceiverType;
string closeDate = receiverPoint.CloseDate;
string createDate = receiverPoint.CreateDate;
string createWho = receiverPoint.CreateWho;
string lastModifyDate = receiverPoint.LastModifyDate;
Console.WriteLine("String lat : " + latitude);
Console.WriteLine("String lon : " + longitude);
Console.WriteLine("String create date : " + createDate);
Console.WriteLine("String create who : " + createWho);
//Save the data to the db
//saveDataToDatabase(station, line, elevation, latitude, longitude, isDeployed, easting, receiverType, closeDate, createDate, createWho, lastModifyDate);
receiverList.Add(receiverPoint);
}
return receiverList;
}
Move receiverList.Add(receiverPoint) inside the foreach loop.

Generate One XML(LogData.xml) file from Two different Application Without Threading

I want to write XML Log File from two or more Application into LogData.xml file. while running the one application it creats the LogData.xml file correctly but at the same time both the allpication are run simulteniously and try to write the LogData.xml file it gives me an error message such as The process cannot access the file Log_Data.xml' because it is being used by another process.
I use this code
public void WriteXmlLog(string logType, string logFlag, string logModule, string logLocation, string logText, string logStackTrace)
{
if (!File.Exists(_logFilePath))
{
//File.WriteAllText(_logFilePath, "<?xml version='1.0' encoding='utf-8' standalone='yes'?>\r\n<AppXmlLogWritter></AppXmlLogWritter>");
XmlTextWriter textWritter = new XmlTextWriter(_logFilePath, null);
textWritter.WriteStartDocument();
textWritter.WriteStartElement("AppXmlLogWritter");
textWritter.WriteEndElement();
textWritter.Close();
}
XmlDocument xmlDoc = new XmlDocument();
using (FileStream fileStream = new FileStream(_logFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
{
string currentDateTime = DateTime.Now.ToString("yyyyMMddHHmmss");
xmlDoc.Load(fileStream);
XmlElement newelement = xmlDoc.CreateElement("LogData");
XmlElement xmlLogID = xmlDoc.CreateElement("LogID");
XmlElement xmlLogDateTime = xmlDoc.CreateElement("LogDateTime");
XmlElement xmlLogType = xmlDoc.CreateElement("LogType");
XmlElement xmlLogFlag = xmlDoc.CreateElement("LogFlag");
XmlElement xmlLogApplication = xmlDoc.CreateElement("LogApplication");
XmlElement xmlLogModule = xmlDoc.CreateElement("LogModule");
XmlElement xmlLogLocation = xmlDoc.CreateElement("LogLocation");
XmlElement xmlLogText = xmlDoc.CreateElement("LogText");
XmlElement xmlLogStackTrace = xmlDoc.CreateElement("LogStackTrace");
xmlLogID.InnerText = _logIDPrefix + currentDateTime + randomNumber;
xmlLogDateTime.InnerText = currentDateTime;
xmlLogType.InnerText = ((LogTypes)Convert.ToInt32(logType)).ToString();
xmlLogFlag.InnerText = logFlag;
xmlLogApplication.InnerText = _logApplication;
xmlLogModule.InnerText = logModule;
xmlLogLocation.InnerText = logLocation;
xmlLogText.InnerText = logText;
xmlLogStackTrace.InnerText = logStackTrace;
newelement.AppendChild(xmlLogID);
newelement.AppendChild(xmlLogDateTime);
newelement.AppendChild(xmlLogType);
newelement.AppendChild(xmlLogFlag);
newelement.AppendChild(xmlLogApplication);
newelement.AppendChild(xmlLogModule);
newelement.AppendChild(xmlLogLocation);
newelement.AppendChild(xmlLogText);
xmlDoc.DocumentElement.AppendChild(newelement);
//}
//finally
//{
// objMutex.ReleaseMutex();
//}
}
xmlDoc.Save(_logFilePath);
}
I want to achive this without Threading
Perhaps try implementing logging with NLog in both applications and set as the target the same xml.

Exception thrown when doing a SOAP call to SharePoint

I am getting this exception when I try to run my application:
Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown
I am making a soap call to SharePoint and it is choking when the soap call is being executed.
below is the code I am running any ideas why this is happening?
public string getListData()
{
Lists myservice = new Lists();
myservice.Credentials = System.Net.CredentialCache.DefaultCredentials;
try
{
/* Assign values to pass the GetListItems method*/
string listName = "*list name*";
string viewName = "*view name*";
string rowLimit = "100";
//string successtest;
//string failtest;
// Instantiate an XmlDocument object
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
System.Xml.XmlElement query = xmlDoc.CreateElement("Query");
System.Xml.XmlElement viewFields = xmlDoc.CreateElement("ViewFields");
System.Xml.XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");
/*Use CAML query*/
query.InnerXml = "<Where><Gt><FieldRef Name=\"ID\" />" + "<Value Type=\"Counter\">0</Value></Gt></Where>";
viewFields.InnerXml = "<FieldRef Name=\"Title\" />";
queryOptions.InnerXml = "";
viewFields.InnerXml = "<FieldRef Name=\"Name\" />";
queryOptions.InnerXml = "";
System.Xml.XmlNode nodes = myservice.GetListItems(listName, viewName, query, viewFields, rowLimit, null, null);
foreach (System.Xml.XmlNode node in nodes)
{
if (node.Name == "rs:data")
{
for (int i = 0; i < node.ChildNodes.Count; i++)
{
if (node.ChildNodes[i].Name == "z:row")
{
//List<String> testList;
test = node.ChildNodes[i].Attributes["ows_Title"].Value;
//Console.WriteLine(node.ChildNodes[i].Attributes["ows_Title"].Value + "</br>");
}
}
}
}
}
catch (Microsoft.SharePoint.SoapServer.SoapServerException ex)
{
test = ex.Detail.InnerText;
//Console.WriteLine(ex.Message);
}
return test;
}

Categories

Resources