Hi,
you can simplified your code. If you serialize with XmlSerializer you can use XmlSerializer to deserialize: (whole demo: x.txt)
#pragma warning disable CS8601 // Dereference of a possibly null reference.
#pragma warning disable CS8602 // Dereference of a possibly null reference.
private void SaveProductsPriceList() //to read xml fiel contents
{
var serializer = new XmlSerializer(typeof(ObservableCollection<Products>));
using (var writer = new XmlTextWriter(FilePath, Encoding.UTF8))
serializer.Serialize(writer, ProductPriceList);
}
#pragma warning disable CS8603 // Possible null reference return.
private ObservableCollection<Products> GetProductsPriceList() //to read xml fiel contents
{
var serializer = new XmlSerializer(typeof(ObservableCollection<Products>));
using (Stream str = new FileStream(FilePath, FileMode.Open))
return (ObservableCollection<Products>)serializer.Deserialize(str);
// old wrong version
//var mylist = new ObservableCollection<Products>();
//XmlDocument doc = new XmlDocument();
//doc.Load(FilePath);
//foreach (XmlElement pn in doc.SelectNodes("/Products"))
//{
// var productlist = new Products
// {
// Mainproduct = pn.LocalName.ToString(),
// Name = pn.GetAttribute("Name"),
// Price = pn.SelectSingleNode(".//ProductPrice/@Price")?.Value,
// Visible = pn.SelectSingleNode(".//ProductVisibility/@Visible")?.Value,
// NameIcon = pn.GetAttribute("DefaultIconName")
// };
// mylist.Add(productlist);
//}
//return mylist;
}
Hi,
your XML-file you cannot deserialize with XmlSerializer. In this case you can use XElement in Get method:
Hi @Peter Fleischer (former MVP) thanks so much for your reply, i already have the method to read the xml contents, my issues is saving back to the file from datagrid? Could you please help me out? thnaks a lot
Tried below code but i have xml exception error when i tried to save Price & visibilty nodes from datagrid back to the xml file.
Hi @Peter Fleischer (former MVP) : thanks so much for your reply, i already have the method to get xml conets to load to the dtagrid.
My issue is to save back to the file from datagrid, i have tried with below but i'm getting xml exception error (i think i'm not inserting proeprly to the xml file), could you please help me out? thanks
The below throws me an error, system.xml.exception:
Error message:
existing method to get xml contents:
Hi,
at first you must declare:
You have one external data structure for loading and another external data structure after serializing with XmlSerializer. In your program you use as backing cache XmlDocument and/or ObservableCollection. Please, declare everything clearly.
Hi there @Peter Fleischer (former MVP) thanks for your additional feedback.
as i need to update Price & Visibility values from datagrid back to XML file.
ProductVisibility
I'm not using serilizing method, to read the xml file i go through nodes/ or i can use your method descendants to get the values from XML file uinsg GetProductsPriceList() method.
XMLdocument to load the xml file to read the file contents, Observable collection for filter (as i learnt from you...)
I got stuck how to go through each node (mentioned above) and update/save the datagrid modified values back to the xml file.
Thanks again for your reply/help.
Hi,
try following classes:
HI @Peter Fleischer (former MVP) thanks so much for your reply. How can i write the Mainproduct, Name & NameIcon with Set property? (I'm not faimilar with Lamda way of setting up gte/set)
Part of my project, i need to export the datagrid to excel then i read the excel file comes form the user export to datagrid using ExcelMapper library which works fine but i need
Mainproduct, Name & NameIcon with Set property ?
Could you please show me how can we write the these three properties with get/set? Again thanks so much for your help/time.
I tried to write the mainproduct propery with get/set but i'm getting an error message its a read only , propery or index cannot be assigned.
Hi,
try this Products class for changing all existing values and attributes.
Hi @Peter Fleischer (former MVP) I really appreciate your help.
I'm getting following errors, what should be done? thanks for your help/time.
Hi,
your class Products is wrong for this use.
Hi @Peter Fleischer (former MVP) ok thanks. In this case do i need to rewite the class or add a default constructor to make it work? if so how? thanks so much
Hi,
ExcelMapper works with POCO classes. My posted class isn't POCO class and is connected to XmlDocument. You must redefine your program logic. I don't know your goal of your solution and cannot help. Please, declare:
Hi @Peter Fleischer (former MVP) noted & thanks for your reply. Poco means plain old c# object?
My goal:
Price & Visible attributes
attributes Price & Visible
imported contents from excel file (this is why i'm using ExcelMapper library)What is the best approach? thanks so much for your help/time
Hi,
what is the primary data store (items with structure of members)? XML file?
If you load data and want to save changed data in the same primary data structure you must hold this data in your solution and identify each data item in datagrid and in Excel. How will you solve this? Excel works without primary keys!
Is full CRUD for this data planned? For full CRUD you must declare technology for creating and deleting nodes in XML file. This is not simple in XML files.
I don't know ExcelMapper and would use Excel object automation.
@Peter Fleischer (former MVP) thanks again. Since its complicated as you said so im trying the below approach to write back to the file, but it dosent work .... i think its go through each product apply the new values to each
attribute Visible & Price
I dont want to create/delete the node i just want to read/update the attributes of the nodes.
Could you please tell me what is wrong with the below approach? thanks
Hi,
for each item in productpricelist (Products pp in productpricelist) you change values in all nodes of XmlDocument. There's no condition in your code. After executing your code all nodes in XMLDocument have the values from last item in productpricelist. You must identify each product in XMLDocument with identifier for each product in productpricelist and change (in each cycle) only node (in XMLDocument) with right identifier.
@Peter Fleischer (Freelancer) , Hi there, great feedback, you're correct it takes the last item from pp and updates to all pn as you said. I guess i have to check,
if the product in pp equals the product in pn
? Any clue about how can i compare with the idetifiers as you mentioned? thanks@Peter Fleischer (former MVP) Hi there: I tried as below as per your usggestion `
but its applying only for a few nodes ; what do you think? thank so much
@Peter Fleischer (former MVP) Hi there, as you said i have added a new field in the class, then i found an identifier for each product
if (pp.SysId == pn.GetAttribute("Sys_Oid").ToString())
to update the node. I'm really sorry that i took so much of your time on this topic, sometimes i think too much at the end the solution is very simple :( . Sorry again & have a good evening.Sign in to comment
0 additional answers
Sort by: Most helpful
Activity