TinyXML2 and C++ - c++

I want to read the data from a wxGrid and write it into a XML File.
The wxGrid is like:
Jahr | Monat
------ |-------------
2012 | 03
2009 | 08
What I want to have:
<SQL>
<Datensatz>
<Jahr>2012</Jahr>
<Monat>03</Monat>
</Datensatz>
<Datensatz>
<Jahr>2009</Jahr>
<Monat>08</Monat>
</Datensatz>
</SQL>
What I got:
<SQL>
<Datensatz>
<Jahr>20122009</Jahr>
<Monat>0308</Monat>
</Datensatz>
<Datensatz>
<Jahr>20122009</Jahr>
<Monat>0308</Monat>
</Datensatz>
</SQL>
My Code:
XMLDocument doc;
XMLElement* xesql = doc.NewElement("SQL");
XMLNode * xnsql = doc.InsertFirstChild(xesql);
XMLElement* xejahr = doc.NewElement("Jahr");
XMLElement* xemonat = doc.NewElement("Monat");
XMLText* datensatzJahr = doc.NewText("");
XMLText* datensatzMonat = doc.NewText("");
for(int i=0; i<=1; i++)
{
XMLElement* xedatensatz = doc.NewElement("Datensatz");
datensatzJahr = doc.NewText(m_gd_data->GetCellValue(i,0));
datensatzMonat = doc.NewText(m_gd_data->GetCellValue(i,1));
xejahr->InsertEndChild(datensatzJahr);
xemonat->InsertEndChild(datensatzMonat);
xedatensatz->InsertEndChild(xejahr);
xedatensatz->InsertEndChild(xemonat);
xesql->InsertEndChild(xedatensatz);
}
doc.SaveFile(path);
I really don't know where's the problem. Can anyone help?

You are not resetting the XML elements for each iteration of the loop, hence you are only appending text to an existing element. this should work:
XMLDocument doc;
XMLElement* xesql = doc.NewElement("SQL");
XMLNode * xnsql = doc.InsertFirstChild(xesql);
for(int i=0; i<=1; i++)
{
XMLElement* xejahr = doc.NewElement("Jahr");
XMLElement* xemonat = doc.NewElement("Monat");
XMLText* datensatzJahr = doc.NewText("");
XMLText* datensatzMonat = doc.NewText("");
XMLElement* xedatensatz = doc.NewElement("Datensatz");
datensatzJahr = doc.NewText(m_gd_data->GetCellValue(i,0));
datensatzMonat = doc.NewText(m_gd_data->GetCellValue(i,1));
xejahr->InsertEndChild(datensatzJahr);
xemonat->InsertEndChild(datensatzMonat);
xedatensatz->InsertEndChild(xejahr);
xedatensatz->InsertEndChild(xemonat);
xesql->InsertEndChild(xedatensatz);
}
doc.SaveFile(path);

You have to create new elements for the year and month inside the loop.

Related

Best way to refactor If-statement

I have the following code:
if (adSetting.Core_standard_application_role)
{
rc = new IntegrationRoleCompany();
rc.RoleCompany = firmSettings.FirmNo.ToString();
rc.RoleName = "Core standard application role";
rcList.Add(rc);
}
if (adSetting.Expense_Invoice_Application_Access)
{
rc = new IntegrationRoleCompany();
rc.RoleCompany = firmSettings.FirmNo.ToString();
rc.RoleName = "Expense Invoice Application Access";
rcList.Add(rc);
}
The problem is that I have 20 if-checks where I check addSetting.Property. Now to the question:
What is the best and most effective way to refactor this if-statements?
you could have a 20x2 array with the adSetting.STUFF_AS_STRING -> rc.RoleName mapping.
then loop over the array in a for loop
pseudo code:
for (var i=0; i<theArray.length; i++) {
adSettingStr, RoleName = theArray[i]
if (adSetting[adSettingStr]) {
rc = new IntegrationRoleCompany();
rc.RoleCompany = firmSettings.FirmNo.ToString();
rc.RoleName = RoleName;
rcList.Add(rc);
}
}

mvc 5 does not delete a cookie

I am trying to delete all cookies from the responder,
the function is inside a service project.
code:
int limit = HttpContext.Current.Request.Cookies.Count;
for (int i = 0; i < limit; i++)
{
cookieName = HttpContext.Current.Request.Cookies[i].Name;
aCookie = new HttpCookie(cookieName);
aCookie.Value = null;
aCookie.Expires = DateTime.Now.AddYears(-1);
HttpContext.Current.Response.Cookies.Add(aCookie)
}
but the response only adds more cookies with the same name. How do I fix this?
Try this:
string[] myCookies = Request.Cookies.AllKeys;
foreach (string cookie in myCookies)
{
Response.Cookies[cookie].Expires = DateTime.Now.AddDays(-1);
}
Edit: this suggestion is based on the guestimate that it has to do with you adding the cookie to the response instead of updating the one that's already present.
after a lot of head banging I finaly noticed that while creating the cookie as followed it did the trick
cookie = new HttpCookie(CookieName, CustId);
declaring the cookie in any other manner didn't work,
for example :
int limit = HttpContext.Current.Request.Cookies.Count;
for (int i = 0; i < limit; i++)
{
cookieName = HttpContext.Current.Request.Cookies[i].Name;
aCookie = new HttpCookie(cookieName);
aCookie.Value = null;
aCookie.Expires = DateTime.Now.AddYears(-1);
HttpContext.Current.Response.Cookies.Add(aCookie);
}
or
var cookie = HttpContext.Current.Response.Cookies[CookieName];
if(cookie == null)
{
cookie = new HttpCookie(CookieName, CustId);
}
else
{
cookie.Expires = DateTime.Now.AddDays(-100);
HttpContext.Current.Response.Cookies.Add(cookie);
//HttpContext.Current.Session.Clear();
}

CCParticleSystemQuand not dealloced

I want to do a very simple thing but it's not working. I want to add some CCParticleSystemQuad in an NSMutableArray and delete them. Here is what I do :
int cpt = 0;
NSMutableArray *myArray = [[NSMutableArray alloc] init];
for (cpt = 0; cpt < 10; cpt++) {
part = [CCParticleSystemQuad particleWithFile:#"whiteExplosion.plist"];
[myArray addObject:part];
}
NSLog(#"state of myArray : %#", myArray);
int cont = 0
for (cont = 0; cont < 10; cont++) {
[myArray removeLastObject:cont];
}
NSLog(#"state of myArray : %#", myArray);
When I NSLog the first time I have this :
state of myArray : (
"<CCParticleSystemQuad = 0x91ee380 | Tag = -1>",
"<CCParticleSystemQuad = 0x84aca20 | Tag = -1>",
"<CCParticleSystemQuad = 0x125136c0 | Tag = -1>",
"<CCParticleSystemQuad = 0x125b0fc0 | Tag = -1>",
"<CCParticleSystemQuad = 0x1250d480 | Tag = -1>",
"<CCParticleSystemQuad = 0x1250fa50 | Tag = -1>",
"<CCParticleSystemQuad = 0x9108840 | Tag = -1>",
"<CCParticleSystemQuad = 0x9152b70 | Tag = -1>",
"<CCParticleSystemQuad = 0x914fb80 | Tag = -1>",
"<CCParticleSystemQuad = 0x9135470 | Tag = -1>"
)
The second time I have this :
state of myArray : (
)
So, as you can see my CCParticleSystemQuad have been removed. But, when I check in Instruments (allocations) they are still living (I have 20 still living [CCParticleSystemQuad allocMemory]) and still using memory for nothing. What am I missing ? BTW I use ARC.
I tried with (NSString *) object and it works fine... Thx.
[myArray removeLastObject:cont];
You're trying to delete an int from the array. You wanted to use removeLastObject (no params) or removeObjectAtIndex:
Your problem may be that you're not calling removeChild:particle
You must remove it from its parent not only from array.
[part removeFromParentAndCleanup:YES];
//to remove object from array
for (int i = 0; i < [myArray count]; i++) {
[myArray removeObjectAtIndex:i];
}
//remove only last object
[myArray removeObjectAtIndex:([myArray count]-1)];

TinyXML2 C++ Import

I've got a XML File looking like:
<?xml version="1.0" encoding="UTF-16"?>
<Table>
<Dataset>
<Year>Year1</Year>
<Month>Month1</Month>
<Day>Day1</Day>
</Dataset>
<Dataset>
<Year>Year2</Year>
<Month>Month2</Month>
<Day>Day1</Day>
</Dataset>
</Table>
And I want to read this file with C++. My code looks like:
XMLElement* xeTable = xeExport->FirstChildElement("Table");
XMLElement* xeDataset = xeTable->FirstChildElement("Dataset");
XMLElement* xeYear = xeDataset->FirstChildElement("Year");
XMLElement* xeMonth = xeDataset->FirstChildElement("Month");
XMLElement* xeDay = xeDataset->FirstChildElement("Day");
XMLText* xnYear = xeYear->FirstChild()->ToText();
const char* cYear = xnYear->Value();
XMLText* xnMonth = xeMonth->FirstChild()->ToText();
const char* cMonth = xnMonth->Value();
XMLText* xnDay = xeDay->FirstChild()->ToText();
const char* cDay = xnDay->Value();
It reads year, month and date of first dataset. What to do know, for reading data of next dataset? I superiored to delete the first dataset after reading so I can read the second dataset again with FirstChildElement();. But I didn't get it.
Can anyone help?
xeDataset = xeDataset->NextSiblingElement("Dataset")
to elaborate:
XMLElement* xeTable = xeExport->FirstChildElement("Table");
for(XMLElement* xeDataset = xeTable->FirstChildElement("Dataset"); xeDataset; xeDataset = xeDataset->NextSiblingElement("Dataset"))
{
XMLElement* xeYear = xeDataset->FirstChildElement("Year");
XMLElement* xeMonth = xeDataset->FirstChildElement("Month");
XMLElement* xeDay = xeDataset->FirstChildElement("Day");
XMLText* xnYear = xeYear->FirstChild()->ToText();
const char* cYear = xnYear->Value();
XMLText* xnMonth = xeMonth->FirstChild()->ToText();
const char* cMonth = xnMonth->Value();
XMLText* xnDay = xeDay->FirstChild()->ToText();
const char* cDay = xnDay->Value();
}

List of windows users remotely

I would like to know how to retrieve the list of users that are logged onto a Remote machine. I can do it with qwinsta /server:xxxx, but would like to do it in C#.
check out wmi in .net under system.management.
something like:
ConnectionOptions conn = new ConnectionOptions();
conn.Authority = "ntdlmdomain:NAMEOFDOMAIN";
conn.Username = "";
conn.Password = "";
ManagementScope ms = new ManagementScope(#"\\remotecomputer\root\cimv2", conn);
ms.Connect();
ObjectQuery qry = new ObjectQuery("select * from Win32_ComputerSystem");
ManagementObjectSearcher search = new ManagementObjectSearcher(ms, qry);
ManagementObjectCollection return = search.Get();
foreach (ManagementObject rec in return)
{
Console.WriteLine("Logged in user: " + rec["UserName"].ToString());
}
You may not need the ConnectionOptions...
I ended up using the qwinsta /server:server1 command from C# -- it was much easier
thanks Ken
All this checks all 8 servers at the same time -- I put the result in sql server
but you can do what ever you want
query_citrix.bat script
cd C:.......\bin\citrix_boxes
qwinsta -server:servername or ip > servername.txt
string sAppCitrixPath = Application.StartupPath.ToString() + "\\citrix_boxes\\";
//Run Script for current citrix boxes
Process proc = new Process();
ProcessStartInfo si = new ProcessStartInfo();
si.FileName = sAppCitrixPath + "query_citrix.bat";
proc.StartInfo = si;
proc.Start();
proc.WaitForExit();
int exitCode = proc.ExitCode;
proc.Close();
if (exitCode == 0)
{
//Execute update who is on the Citrix_Boxes Currently
DirectoryInfo dic = new DirectoryInfo(sAppCitrixPath);
FileInfo[] fic = dic.GetFiles("*.txt");
for (int i = 0; i < fic.Length; i++)
{
ParseQWinStaServerFile(fic[i].FullName.ToString(), fic[i].Name.ToString().ToUpper().Replace(".TXT",""));
File.Delete(fic[i].FullName.ToString());
}
}
private void ParseQWinStaServerFile(string sLocation,string sServer)
{
using (StreamReader sr = File.OpenText(sLocation))
{
string sRecord = String.Empty;
char[] cSep = new char[] {' '};
bool bFirst = true;
while ((sRecord = sr.ReadLine()) != null)
{
if (bFirst == false)
{
string[] items = sRecord.Split(cSep, StringSplitOptions.RemoveEmptyEntries);
//Make sure all columns are present on the split for valid records
if (sRecord.Substring(19, 1) != " ") // check position of user id to see if it's their
{
//Send the user id and server name where you want to.
//here is your user
id = items[1].ToString().ToLower().Trim()
//here is your server
};
}
else
{
bFirst = false;
}
}
}
}