C# Custom Json using JSON.NET from dataset or datatable - c#

Can somebody help me to get the custom JSON string from datatable using C#.
I need something like below.I am able to achive this by using for each loop.
[
{"message": {"alert": "Address Updated"},"target": {"userIds": ["BKAC7759"]}},
{"message": {"alert": "Payment Processed"},"target": {"userIds": ["BKAC7759"]}},
{"message": {"alert": "Notice Sent"},"target": {"userIds": ["BKAC7759"]}}
]
But is there any way i can make it in simple way. My datatable contains values for "alert" and "userIds"
private string GetJsonData(int numberofRecords)
{
// OleDbConnection conn = new OleDbConnection(connectionString);
try
{
DataTable Test = new DataTable("A");
Test.Columns.Add("alert");
Test.Columns.Add("userIds");
Test.Rows.Add("Address Updated", "BKAC7759");
Test.Rows.Add("Payment Made", "BKAC7759");
//Test.Rows.Add("Check Processed", "MAND1884");
//Test.Rows.Add("Notice Mailed", "JAID3869");
//Test.Rows.Add("DL Suspended", "AOQU4798");
string jo = string.Empty;
string com = ",";
int i = 0;
int count = Test.Rows.Count;
string bracketright = "]";
string bracketleft = "[";
foreach (DataRow row in Test.Rows)
{
if (i == 0)
{
jo = jo + bracketleft;
}
jo = jo + "{\"message\":{\"alert\":\"" + row[0].ToString() + "\"},\"target\":{\"userIds\":[\"" + row[1].ToString() + "\"]}}";
if (i != count - 1)
{
jo = jo + com;
}
else
{
jo = jo + bracketright;
}
i++;
}
return jo;
}
catch (Exception ex)
{
Logger.Error("GetJsonData(int numberofRecords): " + ex.Message);
return string.Empty;
}
finally
{
// always close the connection.
// conn.Close();
}
}

You can use Linq + DataTableExtensions (in namespace System.Data and system DLL System.Data.DataSetExtensions.dll) to transform your table into an enumerable of anonymous types, then serialize that to JSON with json.net.
I notice your "userIds" property is a JSON array. Do you want all the user userIds for a given alert to be combined? If so, you can use ToLookup to combine them:
var root = dataTable.AsEnumerable()
.ToLookup(r => r["alert"].ToString(), r => r["userIds"].ToString())
.Select(g => new { message = new { alert = g.Key }, target = new { userIds = g } });
var json = JsonConvert.SerializeObject(root);
If not, do:
var root = dataTable.AsEnumerable()
.Select(r => new { message = new { alert = r["alert"].ToString() }, target = new { userIds = new [] { r["userIds"].ToString() } } });
var json = JsonConvert.SerializeObject(root);
For the following table:
var dataTable = new DataTable("A");
dataTable.Columns.Add("alert");
dataTable.Columns.Add("userIds");
dataTable.Rows.Add("Address Updated", "BKAC7759");
dataTable.Rows.Add("Payment Made", "BKAC7759");
dataTable.Rows.Add("Address Updated", "MAND1884");
dataTable.Rows.Add("Payment Made", "MAND1884");
The first produces the following JSON:
[
{"message":{"alert":"Address Updated"},"target":{"userIds":["BKAC7759","MAND1884"]}},
{"message":{"alert":"Payment Made"},"target":{"userIds":["BKAC7759","MAND1884"]}}
]
And the second produces the following:
[
{"message":{"alert":"Address Updated"},"target":{"userIds":["BKAC7759"]}},
{"message":{"alert":"Payment Made"},"target":{"userIds":["BKAC7759"]}},
{"message":{"alert":"Address Updated"},"target":{"userIds":["MAND1884"]}},
{"message":{"alert":"Payment Made"},"target":{"userIds":["MAND1884"]}}
]

Related

Nest Json data from MySql queries in C#

Im trying to make a nested json with data get from a MySQL DB
For the moment i got
//Obtener el root de info
public List<Root> GetRoot()
{
using (var conexion = ObtenerConexion())
{
conexion.Open();
var query = #"SELECT id AS Id, fullname AS NombreCurso, MID(fullname, INSTR(fullname, ""RLAB""), 20) AS codigoOferta
FROM mo_course
WHERE MID(fullname, INSTR(fullname, ""RLAB""), 20) != '';";
var resultado = conexion.Query<Root>(query);
foreach (var item in resultado)
{
Root root = new Root();
root.codigoOferta = item.codigoOferta;
root.listaAlumnos = new List<ListaAlumno>();
roots.Add(root);
conexion.Close();
}
return roots;
}
}
public List<ListaAlumno> GetAlumnos(string codigoOferta)
{
using (var conexion = ObtenerConexion())
{
conexion.Open();
var query = #"SELECT mo_role_assignments.userid AS Id, firstname AS Nombre, lastname AS Apellido, LEFT(mo_user.idnumber, LENGTH(mo_user.idnumber - 2)) AS rutAlumno,
RIGHT(mo_user.idnumber, 1) AS dvAlumno, email AS Correo, mo_course.fullname AS NombreCurso, mo_course.idnumber AS codigoOferta,
FROM_UNIXTIME(startdate) AS fechaInicio, FROM_UNIXTIME(enddate) AS fechaFin
FROM mo_course
INNER JOIN mo_context ON mo_context.instanceid = mo_course.id
INNER JOIN mo_role_assignments ON mo_context.id = mo_role_assignments.contextid
INNER JOIN mo_user ON (mo_role_assignments.userid = mo_user.id)";
var resultado = conexion.Query<ListaAlumno>(query);
foreach (var item in resultado)
{
ListaAlumno alumno = new ListaAlumno();
alumno.codigoOferta = item.codigoOferta;
alumno.rutAlumno = item.rutAlumno;
alumno.dvAlumno = item.dvAlumno;
alumno.tiempoConectividad = 0;
alumno.estado = 0;
alumno.porcentajeAvance = 0;
alumno.fechaInicio = item.fechaInicio;
alumno.fechaFin = item.fechaFin;
alumno.fechaEjecucion = "";
alumno.listaModulos = new List<ListaModulo>();
listaAlumnos.Add(alumno);
}
conexion.Close();
}
return listaAlumnos;
}
}
but i cant find a way to do the variable to nest the data, since i getting the data as direct query the parameter "codigoOferta" in GetAlumnos is not working
The idea its to do the following
{
"rutOtec": "9999999-9",
"idSistema": 1350,
"token": "AAAAAA-ASSS-DDDDd-8FFF-2FFFFFFFFD2",
"codigoOferta": "RSJ-D444-555-666666",
"codigoGrupo": "RSJ-D444-555-666666",
"codigoEnvio": "RSJ-D444-555-666666",
"listaAlumnos": [
{
"rutAlumno": 255544466,
"dvAlumno": "4",
"tiempoConectividad": 199112,
"estado": 1,
"porcentajeAvance": 99,
"fechaInicio": "2022-07-28",
"fechaFin": "2022-10-24",
"fechaEjecucion": "2022-10-24",
"listaModulos": [
{
"codigoModulo": "ARS027773588877",
"tiempoConectividad": 7964,
"estado": 1,
"porcentajeAvance": 100,
"fechaInicio": "2022-07-28",
"fechaFin": "2022-10-24",
"listaActividades": [
{
"codigoActividad": "Excel"
},
{
"codigoActividad": "Video interactivo de introducci\u00f3n a Excel"
},
but since for each student in listaAlumno the data change i need to nest the variables

Searching and deleting a node from JSON

JSON is holding a data.
I need to remove the node where the "id" value matches passed in value. In the below code var isRemoved = (dataDeserialized as JArray).Remove(index); is returning false. The node is not deleted. How can i delete the node?
The property name "id" is even dynamic. It will get passed in. How can i check the property name along with value (current) to find the index?
Here is what i have so far:
var entityJson = #"
[
{
'id': 1,
'code': 'abc1'
},
{
'id': 2,
'code': 'abc2'
}
]".Replace("'", "\"");
var dataDeserialized = JsonConvert.DeserializeObject<dynamic>(entityJson);// DataLoader.DeserializeJson(entityJson, false);
if(dataDeserialized.Count > 0)
{
var key = "id";
var value = "2";
var index = 0;
var isFound = false;
foreach(var d in dataDeserialized)
{
if((string)d.id == value.ToString())
{
isFound = true;
break;
}
index++;
}
if (isFound)
{
var isRemoved = (dataDeserialized as JArray).Remove(index);
if (isRemoved)
{
var setting = new JsonSerializerSettings
{
DateParseHandling = DateParseHandling.None,
};
var resul = JsonConvert.SerializeObject(dataDeserialized, setting);
//var result = JsonHelper.SerializeObject(dataDeserialized);
}
}
}
I have tried jObject as well but it is giving an error Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 2, position 13
dynamic obj = JObject.Parse(entityJson);
(obj.employees as JArray).RemoveAt(0);
Update #1: here is what worked for me for #1 above
if(dataDeserialized.Count > 0)
{
var key = "id";
var value = "2";
JArray list = new JArray();
foreach (var d in dataDeserialized)
{
if ((string)d.id != value.ToString())
{
list.Add(d);
}
}
if(list.Count > 0)
{
var result = JsonConvert.SerializeObject(list);
}
}
Still, how can i do #2, checking the "key"?
If you don't want to deserialize this json to a strongly typed object, I would recommend working with the JObject and JArray classes from the Newtonsoft.Json package because they are much more flexible for this purpose.
So you can easily remove a node from the json with a dynamic key like this:
string key = "id";
int id = 2;
var entityJson = #"
[
{
""id"": 1,
""code"": ""abc1""
},
{
""id"": 2,
""code"": ""abc2""
},
{
""id"": 3,
""code"": ""abc3""
}
]";
JArray jarray = JArray.Parse(entityJson);
var item = jarray.Children().Where(i => (int)(i as JObject).GetValue(key) == id).SingleOrDefault();
if (item != null)
{
jarray.Remove(item);
}
var resul = JsonConvert.SerializeObject(jarray);

C# NEST query does not print out data correctly

I'm stuck at a problem, apparently I might have misunderstood something, but here is the problem.
I've got some testdata in a database, but when I run this:
Connection to elastic
private string uri = "http://localhost:9200/";
private string testindex = "testconnectiones";
private static ElasticClient GetClient(string testindex, string uri)
{
var pool = new SingleNodeConnectionPool(new Uri(uri));
var connectionSettings =
new ConnectionSettings(pool, sourceSerializer: JsonNetSerializer.Default).DefaultIndex(testindex);
return new ElasticClient(connectionSettings);
}
Then
public void TestCreateIndexBaseOnOrderId1()
{
var client = GetClient(testindex, uri);
var searchResponse = client.Search<TestLogs>(s => s
.AllTypes()
.From(0)
.Size(1000)
.Analyzer("standard")
.Query(q => q
.Match(m => m
.Field(f => f.OrderID)
.Query("")
)
)
);
var eventTestArray = searchResponse.Documents.Select(x => new {x.OrderID }).ToArray();
Console.WriteLine("searchResponse.Documents.Count: " + searchResponse.Documents.Count());
var i = 0;
var j = 0;
foreach (var s in eventTestArray)
{
Console.WriteLine($"{i}: " + s);
i++;
}
Assert.AreNotEqual(eventTestArray, null);
}
the output is only:
searchResponse.Documents.Count: 0.
There should be 10 orders in the search response.
I just wonder if I've misunderstood something.
The data in the database looks for example like this
{
"_index": "testconnectiones",
"_type": "logs",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"OrderId": 1,
"Event": "CreateOrder"
}
}
When using the type TestLogs, how does the client know to target testconnectiones index?
There are a few things to consider:
Ensuring that testconnectiones index is queried
Ensuring that f => f.OrderID will serialize to a string value to target a field that exists for the type and index in Elasticsearch. By default, NEST camel cases property names when serializing
Ensuring that the query has an input so that it is not considered "conditionless", or using .Verbatim() to serialize the query exactly as is
Ensuring that indexed documents are available for search
A part of the problem was now solved went by.
QueryContainer query = new TermQuery()
{
Field = "OrderId",
Value = "1"
};
var searchRequest = new SearchRequest(index: "testindex")
{
Query = query
};
var searchResult = client.Search<TestLogs>(searchRequest);
foreach (var s in orderIdArray)
{
Console.WriteLine($"{i}: OrderId:" + s.OrderID + " Event: " + s.Event + " Time: " + s.TimeStamp);
i++;
}
Now is the problem only that if i change
QueryContainer query = new TermQuery()
{
Field = "OrderId",
Value = "1"
};
to...
QueryContainer query = new TermQuery()
{
Field = "OrderId",
Value = "1"
};
It wont give any output is Test method ELK_algorithmsTests.TestIndexCreation.testToMakeSameSelectionButWithSelectedEvent threw exception:
System.IndexOutOfRangeException: ..
any suggestion where is should look? because im out of ideas right now..

UWP - Insert data from the server (json) into the database with certain conditions

I have an app that checks the data on the server (json) and the database. I want when the id on json is not the same as the id on the database, it will insert all data with unequal id into the database.
Code:
var sqlpath = System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "ebookstore.db");
this.DataContextChanged += (s, e1) => { UpdateViewModel = DataContext as ViewModels.UpdateViewModel; };
string idDb = #"SELECT id FROM books where parent_folder_id = 2 and title like '%guru%'";
var IDdb = objConnUpdate.Prepare(idDb);
IDdb.Step();
iddb = IDdb[0].ToString();
IDDB = Convert.ToInt32(iddb.ToString());
ConnectionProfile connections = NetworkInformation.GetInternetConnectionProfile();
{
try
{
Downloading.IsOpen = true;
string urlPath1 = "https://.../fetch/k13G";
var httpClient1 = new HttpClient(new HttpClientHandler());
httpClient1.DefaultRequestHeaders.TryAddWithoutValidation("KIAT-API-KEY", "...*");
var values1 = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("halaman", "1"),
new KeyValuePair<string, string>("limit", "20"),
};
var response1 = await httpClient1.PostAsync(urlPath1, new FormUrlEncodedContent(values1));
response1.EnsureSuccessStatusCode();
string jsonText1 = await response1.Content.ReadAsStringAsync();
JsonObject jsonObject1 = JsonObject.Parse(jsonText1);
JsonArray jsonData1 = jsonObject1["data"].GetArray();
foreach (JsonValue groupValue in jsonData1)
{
JsonObject groupObject = groupValue.GetObject();
string ID = groupObject["id"].GetString();
BukuUpdate file1 = new BukuUpdate();
file1.ID = ID;
int intID = Convert.ToInt32(file1.ID);
if (intID != IDDB)
{
string jumlahidDb = #"SELECT COUNT(id) FROM books where parent_folder_id = 2 and id > " + IDDB + " and title like '%guru%'";
var jumlahIdDB = objConnUpdate.Prepare(jumlahidDb);
jumlahIdDB.Step();
if (jumlahiddb < jumlahbuku)
{;
if (nama == "Kelas_01_SD_")
{
DownloadBukuK2013G(url);
string K2013GUpdate = #"INSERT INTO books (id,title,folder_id,identifier,parent_folder_id) SELECT " + intID + ",'" + namaFile + ".pdf',34,'" + namaFile +
".pdf',2 WHERE not exists (select id AND title AND folder_id AND identifier AND parent_folder_id FROM books WHERE id=" + intID + " and title='" + namaFile +
".pdf' AND folder_id=34 and identifier='" + namaFile + ".pdf' and parent_folder_id=2)";
var K2013GQuery = objConnUpdate.Prepare(K2013GUpdate);
K2013GQuery.Step();
}
BukuUpdate.cs:
public class BukuUpdate
{
public string ID { get; set; }
}
Database:
I did not succeed in implementing it. How to handle it?
But if I use the code in my previous post, it only shows the first id in the database
You getting the first id by this code line iddb = IDdb[0].ToString(); for using. Actually the id list is saved in idDb variable.
I want if the id on json is not the same as the id on the database, then the data will be added to the database. If the same, then do nothing.
For this, you may just get all the ids from the database, and compare with the new income record from Json one by one. From your code snippet I'm not sure what's the Nuget package you're using for Sqlite, but the newest official tutorial using Microsoft.Data.SQLite package. I also strongly recommended you to use Microsoft.Data.SQLite package by following the official tutorial. By this way, the insert sample code may like this:
public sealed partial class MainPage : Page
{
private void btngettest_Click(object sender, RoutedEventArgs e)
{
List<String> ids = sqlhelp.GetData();
string idfromjson = "2";
foreach (string id in ids)
{
if (id != idfromjson)
{
//do insert operation
}
else
{
//do nothing
}
}
}
}
public class sqlhelp
{
public static void InitializeDatabase()
{
...
}
public static List<String> GetData()
{
List<String> entries = new List<string>();
using (SqliteConnection db =
new SqliteConnection("Filename=sqliteSample.db"))
{
db.Open();
SqliteCommand selectCommand = new SqliteCommand
("SELECT id from MyTable", db);
SqliteDataReader query = selectCommand.ExecuteReader();
while (query.Read())
{
entries.Add(query.GetString(0));
}
db.Close();
}
return entries;
}
Since your code snippet contains a lot of your own logic that I didn't give changes on your original code snippet. Please kindly reference my simple code which may be more clearly to know.

Modify an attribute according to its data type c#

For a giving DB, I used CodeSmith to generate a text file that has the following values
(TableName)([TableGUID]) (AttributeName).(AttributeType)
for example
CO_CallSignLists[e3fc5e2d-fe84-492d-ad94-3acced870714] SunSpots.smallint
Now I parsed these values and assigned each to a certain variable
for (int j = 0; j < newLst.Count; j += 2)
{
test_objectName_Guid = newLst[j]; //CO_CallSignLists[e3fc5e2d-fe84-492d-ad94-3acced870714]
test_attr = newLst[j + 1]; //SunSpots.smallint
//Seperate Guid from objectName
string[] obNameGuid = test_objectName_Guid.Split('[',']');
var items = from line in obNameGuid
select new
{
aobjectName = obNameGuid[0],
aGuid = obNameGuid[1]
};
foreach (var item in items)
{
final_objectName = item.aobjectName;
final_oGuid = new Guid(item.aGuid);
}
Console.WriteLine("\nFinal ObjectName\t{0}\nFinal Guid\t\t{1}",
final_objectName, final_oGuid);
string final_attributeName = string.Empty;
string final_attributeType = string.Empty;
string[] words = test_attr.Split('.');
var items2 = from line in words
select new
{
attributeName = words[0],
attributeType = words[1]
};
foreach (var item in items2)
{
final_attributeName = item.attributeName;
final_attributeType = item.attributeType;
}
Console.WriteLine("Attribute Name\t\t{0}\nAttributeType\t\t{1}\n",
final_attributeName, final_attributeType);
I then generate an xml file that loads data from the DB depending on the objectName and its GUID and save this xml in a string variable
string generatedXMLFile = Test.run_Load_StoredProcedure(final_objectName, final_oGuid, Dir);
Now I wanna modify the attribute in this xml file that is equal to the attribute from the parsed txt file. (I wanna modify it according to its type)
public void modifyPassedAttribute(string myFile, string attributeName)
{
Object objString = (Object)attributeName;
string strType = string.Empty;
int intType = 0;
XDocument myDoc = XDocument.Load(myFile);
var attrib = myDoc.Descendants().Attributes().Where(a => a.Name.LocalName.Equals(objString));
foreach (XAttribute elem in attrib)
{
Console.WriteLine("ATTRIBUTE NAME IS {0} and of Type {1}", elem, elem.Value.GetType());
if (elem.Value.GetType().Equals(strType.GetType()))
{
elem.Value += "_change";
Console.WriteLine("NEW VALUE IS {0}", elem.Value);
}
else if (elem.Value.GetType().Equals(intType.GetType()))
{
elem.Value += 2;
Console.WriteLine("NEW VALUE IS {0}", elem.Value);
}
}
myDoc.Save(myFile);
}
The problem is that I always says that the value type is 'string' and modifies it as a string (adds "_change").. though when I wanna save the data back into the DB it says you can't assign a nvarchar to a smallint value.
What's wrong with my code? Why do I always get a string type? Is it because all attributes are treated as strings in an xml file? How then can I modify the attribute according to its original type so that I won't get errors when I wanna save it back in the DB?
I'm open for suggestion to optimize the code I know it's not the best code to archive my goal
EDIT
Here is the code to generate the XMLs
public void generate_XML_AllTables(string Dir)
{
SqlDataReader Load_SP_List = null; //SQL reader that gets list of stored procedures in the database
SqlDataReader DataclassId = null; //SQL reader to get the DataclassIds from tables
SqlConnection conn = null;
conn = new SqlConnection("Data Source= EUADEVS06\\SS2008;Initial Catalog=TacOps_4_0_0_4_test;integrated security=SSPI; persist security info=False;Trusted_Connection=Yes");
SqlConnection conn_2 = null;
conn_2 = new SqlConnection("Data Source= EUADEVS06\\SS2008;Initial Catalog=TacOps_4_0_0_4_test;integrated security=SSPI; persist security info=False;Trusted_Connection=Yes");
SqlCommand getDataclassId_FromTables;
int num_SP = 0, num_Tables = 0;
string strDataClass; //Name of table
string sql_str; //SQL command to get
conn.Open();
//Select Stored Procedeurs that call upon Tables in the DB. Tables which have multiple DataClassIds (rows)
//Selecting all Load Stored Procedures of CLNT & Get the table names
// to pass the Load operation which generates the XML docs.
SqlCommand cmd = new SqlCommand("Select * from sys.all_objects where type_desc='SQL_STORED_PROCEDURE' and name like 'CLNT%Load';", conn);
Load_SP_List = cmd.ExecuteReader();
while (Load_SP_List.Read())
{
//Gets the list of Stored Procedures, then modifies it
//to get the table names
strDataClass = Load_SP_List[0].ToString();
strDataClass = strDataClass.Replace("CLNT_", "");
strDataClass = strDataClass.Replace("_Load", "");
sql_str = "select TOP 1 DataclassId from " + strDataClass;
conn_2.Open();
getDataclassId_FromTables = new SqlCommand(sql_str, conn_2);
DataclassId = getDataclassId_FromTables.ExecuteReader();
while (DataclassId.Read())
{
string test = DataclassId[0].ToString();
Guid oRootGuid = new Guid(test);
run_Load_StoredProcedure(strDataClass, oRootGuid, Dir);
num_Tables++;
}
DataclassId.Close();
conn_2.Close();
num_SP++;
}
Load_SP_List.Close();
conn.Close();
System.Console.WriteLine("{0} of Stored Procedures have been executed and {1} of XML Files have been generated successfully..", num_SP,num_Tables);
}
public string run_Load_StoredProcedure(string strDataClass, Guid guidRootId, string Dir)
{
SqlDataReader rdr = null;
SqlConnection conn = null;
conn = new SqlConnection("Data Source= EUADEVS06\\SS2008;Initial Catalog=TacOps_4_0_0_4_test;integrated security=SSPI; persist security info=False;Trusted_Connection=Yes");
conn.Open();
// Procedure call with parameters
SqlCommand cmd = new SqlCommand("CLNT_" + strDataClass + "_Load", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 0;
//Adding parameters, in- and output
SqlParameter idParam = new SqlParameter("#DataclassId", SqlDbType.UniqueIdentifier);
idParam.Direction = ParameterDirection.Input;
idParam.Value = guidRootId;
SqlParameter xmlParam = new SqlParameter("#XML", SqlDbType.VarChar, -1 /*MAX*/ );
xmlParam.Direction = ParameterDirection.Output;
cmd.Parameters.Add(idParam);
cmd.Parameters.Add(xmlParam);
rdr = cmd.ExecuteReader(CommandBehavior.SingleResult);
DirectoryInfo dest_2 = new DirectoryInfo(Dir + "\\Copies");
DirectoryInfo dest = new DirectoryInfo(Dir + "\\Backup");
DirectoryInfo source = new DirectoryInfo(Dir);
if (source.Exists == false)
{
source.Create();
if (dest.Exists == false)
{
dest.Create();
}
if (dest_2.Exists == false)
{
dest_2.Create();
}
}
string xmlFile = #Dir + "\\" + strDataClass + " [" + guidRootId + "].xml";
//The value of the output parameter ‘xmlParam’ will be saved in XML format using the StreamWriter.
System.IO.StreamWriter wIn = new System.IO.StreamWriter(xmlFile, false);
wIn.WriteLine(xmlParam.Value.ToString());
wIn.Close();
rdr.Close();
rdr.Close();
conn.Close();
return xmlFile;
}
Short answer:
Is it because all attributes are treated as strings in an xml file?
Yes.
You'll need to store the original type in the Xml - as far as I could tell, you're not including that, so you're discarding the information.
ok so I solved the issue! All I had to do was to pass the attributeType string to the modifyPassedAttribute function and use it to determine the changes I wanna make
Here is the modified final code
public void modifyPassedAttribute(string myFile, string attributeName, string attributeType)
{
Object objString = (Object)attributeName;
string strType = "nvarchar";
string smallintType = "smallint";
string intType = "int";
string dateType = "datetime";
XDocument myDoc = XDocument.Load(myFile);
//var myAttr = from el in myDoc.Root.Elements()
// from attr in el.Attributes()
// where attr.Name.ToString().Equals(attributeName)
// select attr;
var attrib = myDoc.Descendants().Attributes().Where(a => a.Name.LocalName.Equals(objString));
foreach (XAttribute elem in attrib)
{
Console.WriteLine("ATTRIBUTE NAME IS {0} and of Type {1}", elem, elem.Value.GetType());
if (strType.Equals(attributeType))
{
if (elem.Value.EndsWith("_change"))
{
elem.Value = elem.Value.Replace("_change", "");
Console.WriteLine("NEW VALUE IS {0}", elem.Value);
}
else
{
elem.Value += "_change";
Console.WriteLine("NEW VALUE IS {0}", elem.Value);
}
}
else if (smallintType.Equals(attributeType))
{
if (elem.Value.EndsWith("2"))
{
elem.Value = elem.Value.Replace("2", "");
Console.WriteLine("NEW VALUE IS {0}", elem.Value);
}
else
{
elem.Value += 2;
Console.WriteLine("NEW VALUE IS {0}", elem.Value);
}
}
else if (intType.Equals(attributeType))
{
if (elem.Value.EndsWith("2"))
{
elem.Value = elem.Value.Replace("2", "");
Console.WriteLine("NEW VALUE IS {0}", elem.Value);
}
else
{
elem.Value += 2;
Console.WriteLine("NEW VALUE IS {0}", elem.Value);
}
}
else if (dateType.Equals(attributeType))
{
if (elem.Value.EndsWith("2"))
{
elem.Value = elem.Value.Replace("2", "");
Console.WriteLine("NEW VALUE IS {0}", elem.Value);
}
else
{
elem.Value += 2;
Console.WriteLine("NEW VALUE IS {0}", elem.Value);
}
}
}
myDoc.Save(myFile);
}
the if statements inside are there so that no large numbers would be generated because the 2 is added to the string 100 (i.e. 1002) and if each time I'm gonna add 2 then it's gonna simply crash

Categories

Resources