Ciao a tutti,
come ci suggerisce il titolo del post, oggi illustrerò un modo comodo per leggere un file ( oppure una stringa ) contenente un messaggio XML per utilizzarlo all'interno del nostro programma......
Innanzitutto, prendiamo un file XML
<XML_NUMORDINE>
<INFORMAZIONI>
<CODICE_PREVENTIVO>2016O000000470</CODICE_PREVENTIVO>
<DATA_AUTORIZZAZIONE>2016-12-01</DATA_AUTORIZZAZIONE>
<NUMERO_ORDINE>1</NUMERO_ORDINE>
<DATA_ORDINE>2016-12-01</DATA_ORDINE>
</INFORMAZIONI>
<INFORMAZIONI>
...
</INFORMAZIONI>
<INFORMAZIONI>
...
</INFORMAZIONI>
<INFORMAZIONI>
...
</INFORMAZIONI>
</XML_NUMORDINE>
La necessità è, come si può immaginare, quella di ricreare un vettore o lista contenente tutti i campi racchiusi nel tag <INFORMAZIONI/>.
Per far ciò utilizzeremo Linq, innanzitutto una classe di comodo che rappresenta i dati :
public class ItemsXmlTibcoPull {
public ItemsXmlTibcoPull() {
CODICE_PREVENTIVO = "";
DATA_AUTORIZZAZIONE="";
NUMERO_ORDINE = "";
DATA_ORDINE = "";
}
public string DATA_ORDINE { get; set; }
public string NUMERO_ORDINE { get; set; }
public string DATA_AUTORIZZAZIONE { get; set; }
public string CODICE_PREVENTIVO { get; set; }
}
In seconda battuta un parser:
//" NOTA:questo parser prevede in ingresso il path del file xml o il suo contenuto"
public List<ItemsXmlTibcoPull> parser(String path, bool isPath = true){
List<ItemsXmlTibcoPull> list = new List<ItemsXmlTibcoPull>();
XElement e =null;
if (isPath) e = XElement.Load(path);
else e = XElement.Load(new System.IO.StringReader(path));
//wrappiamo il tag <INFORMAZIONI/>
foreach (XElement level1Element in e.Elements("INFORMAZIONI"))
{
ItemsXmlTibcoPull item = new ItemsXmlTibcoPull();
//e per ogni elemento ne prendiamo il valore
foreach (XElement level2Element in level1Element.Elements("CODICE_PREVENTIVO"))
{
item.CODICE_PREVENTIVO = level2Element.Value;
}
foreach (XElement level2Element in level1Element.Elements("DATA_AUTORIZZAZIONE"))
{
item.DATA_AUTORIZZAZIONE = level2Element.Value;
}
foreach (XElement level2Element in level1Element.Elements("NUMERO_ORDINE"))
{
item.NUMERO_ORDINE = level2Element.Value;
//qualora avessimo dovuto recuperare un attributo sulla property... name ad esempio
//result.AppendLine(level1Element.Attribute("name").Value);
}
//result.AppendLine(level1Element.Attribute("name").Value);
foreach (XElement level2Element in level1Element.Elements("DATA_ORDINE"))
{
item.DATA_ORDINE = level2Element.Value;
}
list.Add(item);
}
return list;
}
ed il gioco è fatto!!!
Non commento ulteriormente perchè si commenta da solo .....
buon lavoro!
ciao
Visualizzazione post con etichetta C#. Mostra tutti i post
Visualizzazione post con etichetta C#. Mostra tutti i post
giovedì 24 novembre 2016
Parsing XML with C# And LINQ Microsoft Framework 4.0
mercoledì 13 novembre 2013
Crypt And Decrypt rar files with c#
Ciao a tutti,
un link interessante per cryptare e decriptare file rar in c#!!!
http://www.fluxbytes.com/csharp/encrypt-and-decrypt-files-in-c/
Riporto il codice qualora la pagina linkata dovesse essere dismessa.
CRYPT
Decrypt
File Input File Output Key
EncryptFile("C:\\myfile.rar", "c:\\myfileEncrypted.rar", "1234512345678976");
DecryptFile("C:\\myfileEncrypted.rar", "c:\\myfileDecrypted.rar", "1234512345678976");
Ciao e alla prossima!!!
un link interessante per cryptare e decriptare file rar in c#!!!
http://www.fluxbytes.com/csharp/encrypt-and-decrypt-files-in-c/
Riporto il codice qualora la pagina linkata dovesse essere dismessa.
CRYPT
private static void EncryptFile(string inputFile, string outputFile, string skey)
{
try
{
using (RijndaelManaged aes = new RijndaelManaged())
{
byte[] key = ASCIIEncoding.UTF8.GetBytes(skey);
/* This is for demostrating purposes only.
* Ideally you will want the IV key to be different from your key and you should always generate a new one for each encryption in other to achieve maximum security*/
byte[] IV = ASCIIEncoding.UTF8.GetBytes(skey);
using (FileStream fsCrypt = new FileStream(outputFile, FileMode.Create))
{
using (ICryptoTransform encryptor = aes.CreateEncryptor(key, IV))
{
using (CryptoStream cs = new CryptoStream(fsCrypt, encryptor, CryptoStreamMode.Write))
{
using (FileStream fsIn = new FileStream(inputFile, FileMode.Open))
{
int data;
while ((data = fsIn.ReadByte()) != -1)
{
cs.WriteByte((byte)data);
}
}
}
}
}
}
}
catch (Exception ex)
{
// failed to encrypt file
}
}
private static void DecryptFile(string inputFile, string outputFile, string skey)
{
try
{
using (RijndaelManaged aes = new RijndaelManaged())
{
byte[] key = ASCIIEncoding.UTF8.GetBytes(skey);
/* This is for demostrating purposes only.
* Ideally you will want the IV key to be different from your key and you should always generate a new one for each encryption in other to achieve maximum security*/
byte[] IV = ASCIIEncoding.UTF8.GetBytes(skey);
using (FileStream fsCrypt = new FileStream(inputFile, FileMode.Open))
{
using (FileStream fsOut = new FileStream(outputFile, FileMode.Create))
{
using (ICryptoTransform decryptor = aes.CreateDecryptor(key, IV))
{
using (CryptoStream cs = new CryptoStream(fsCrypt, decryptor, CryptoStreamMode.Read))
{
int data;
while ((data = cs.ReadByte()) != -1)
{
fsOut.WriteByte((byte)data);
}
}
}
}
}
}
}
catch (Exception ex)
{
// failed to decrypt file
}
}
Usage
File Input File Output Key
EncryptFile("C:\\myfile.rar", "c:\\myfileEncrypted.rar", "1234512345678976");
DecryptFile("C:\\myfileEncrypted.rar", "c:\\myfileDecrypted.rar", "1234512345678976");
Ciao e alla prossima!!!
Iscriviti a:
Post (Atom)