Set two ObservableCollection equal, without losing WPF-Binding - c#

I have a ObservableCollection of MediaFile
MediaControlClass.GetInstanze().MediaLibrary
I have a lot of WPF Bindings of this ObservableCollection.
In some case I have to refill this collection form a XML File. In the XML File are 20.000 MediaFiles. I tried two ways.
First way:
serializer = new XmlSerializer(typeof(System.Collections.ObjectModel.ObservableCollection<MediaFile>));
XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
xmlReaderSettings.CheckCharacters = false;
using (XmlReader reader = XmlReader.Create(Environment.CurrentDirectory + "\\Notes.xml", xmlReaderSettings))
{
o = serializer.Deserialize(reader);
MediaControlClass.GetInstanze().MediaLibrary.Clear();
foreach (var i in (System.Collections.ObjectModel.ObservableCollection<MediaFile>)o)
MediaControlClass.GetInstanze().MediaLibrary.Add(i);
}
I added every MediaFile to the MediaControlClass.GetInstanze().MediaLibrary which was really to slow.
Second way:
serializer = new XmlSerializer(typeof(System.Collections.ObjectModel.ObservableCollection<MediaFile>));
XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
xmlReaderSettings.CheckCharacters = false;
using (XmlReader reader = XmlReader.Create(Environment.CurrentDirectory + "\\Notes.xml", xmlReaderSettings))
{
o = serializer.Deserialize(reader);
MediaControlClass.GetInstanze().MediaLibrary = (System.Collections.ObjectModel.ObservableCollection<MediaFile>)o;
}
This is fast, but I have to rebind a lot of WPF Controls.
Is there some way to do this fast and without rebinding?

This is just a wild guess, but would raising a manual property changed notification help in the second case?
As for the first method, might be that it's slow because you're doing a typecast inside foreach and it casts on every loop?

I agree with dain. If you raise a OnPropertyChanged event in MediaLibrary setter, you should not have to move any binding.

Related

Deserialize object one by one from file .Net

I'm trying to deserialize a list of heavy objects from a json file. I do not want to deserialize it the classic way, like directly to a list, because it will expose me to an OutOfMemory exception. So I'm looking for a way to handle object one by one to store them one by one in the database and be memory safe.
I already handle the serialization and it's working well, but I'm facing some difficulties for deserialization.
Any idea ?
Thanks in advance
// Serialization
using (var FileStream = new FileStream(DirPath + "/TPV.Json", FileMode.Create))
{
using (var sw = new StreamWriter(FileStream))
{
using (var jw = new JsonTextWriter(sw))
{
jw.WriteStartArray();
using (var _Database = new InspectionBatimentsDataContext(TheBrain.DBClient.ConnectionString))
{
foreach (var TPVId in TPVIds)
{
var pic = (from p in _Database.TPV
where Operators.ConditionalCompareObjectEqual(p.Release, TPVId.Release, false) & Operators.ConditionalCompareObjectEqual(p.InterventionId, TPVId.InterventionId, false)
select p).FirstOrDefault;
var ser = new JsonSerializer();
ser.Serialize(jw, pic);
jw.Flush();
}
}
jw.WriteEndArray();
}
}
}
I finnaly found a way to do it by using custom separator beetween each object during serialization. Then for deserialization, I simply read the json file as string until I find my custom separator and I deserialise readed string, all in a loop. It's not the perfect answer because I'm breaking json format in my files, but it's not a constraint in my case.

Can't set settings on XmlTextReader?

I need to set the MaxCharactersFromEntities on the XmlTextReader, this is my code so far :
xmlDocument = new XmlDocument();
xmlTextReader = new XmlTextReader(fileInfo.FullName);
xmlTextReader.Settings = new XmlReaderSettings();
xmlTextReader.Settings.MaxCharactersFromEntities = 0;
var vr = new XmlValidatingReader(xmlTextReader);
vr.ValidationType = ValidationType.None;
vr.EntityHandling = EntityHandling.ExpandEntities;
xmlDocument.Load(vr);
The Settings property is read-only so it canĀ“t be set and its null? How is this supposed to work?
You're supposed to pass XmlReaderSettings instance upon construction of the XmlReader instance in the first place, rather than updating the reader's Settings property later -which is impossible as the property doesn't have setter- :
var xmlDocument = new XmlDocument();
//create XmlReaderSettings first
var settings = new XmlReaderSettings();
settings.MaxCharactersFromEntities = 80; //0 doesn't make sense here, as it's the default value
//create XmlReader later, passing the pre-defined settings
var xmlReader = XmlReader.Create(fileInfo.FullName, settings);
//the rest of the codes remain untouched
var vr = new XmlValidatingReader(xmlReader);
vr.ValidationType = ValidationType.None;
vr.EntityHandling = EntityHandling.ExpandEntities;
xmlDocument.Load(vr);
You should use XmlReader.Create(string, XmlReaderSettings) instead to create your reader instance.
From the MSDN reference:
Starting with the .NET Framework 2.0, we recommend that you use the
System.Xml.XmlReader class instead.
The idea is to use the Create(...) factory method of the base class XmlReader instead of instantiating the derived class directly. Also see the factory method pattern for additional info.
The rest of your code remains unaffected since XmlValidatingReader takes an XmlReader in the constructor.
So you should end up with somehting like:
xmlDocument = new XmlDocument();
XmlReaderSettings settings = new XmlReaderSettings();
settings.MaxCharactersFromEntities = 0;
XmlReader reader = XmlReader.Create(fileInfo.FullName, settings);
var vr = new XmlValidatingReader(reader);
vr.ValidationType = ValidationType.None;
vr.EntityHandling = EntityHandling.ExpandEntities;
xmlDocument.Load(vr);

XmlTextWriter serialize Object to XML element

I would like to perform object serialization to only one branch in an existing XML file. While reading by using:
RiskConfiguration AnObject;
XmlSerializer Xml_Serializer = new XmlSerializer(typeof(RiskConfiguration));
XmlTextReader XmlReader = new XmlTextReader(#"d:\Projects\RiskService\WCFRiskService\Web.config");
XmlReader.ReadToDescendant("RiskConfiguration");
try
{
AnObject = (RiskConfiguration)Xml_Serializer.Deserialize(XmlReader);
AnObject.Databases.Database[0].name = "NewName";
}
finally
{
XmlReader.Close();
}
It is possible, I do not know how to edit the object again performed it can save the file without erasing other existing elements in an XML file. Can anyone help me?
I found a way to display the desired item serialization. How do I go now instead of the paste to the original element in XML?
StringWriter wr = new StringWriter();
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = true;
settings.Encoding = System.Text.Encoding.Default;
using (XmlWriter writer = XmlWriter.Create(wr, settings))
{
XmlSerializerNamespaces emptyNamespace = new XmlSerializerNamespaces();
emptyNamespace.Add(String.Empty, String.Empty);
Xml_Serializer.Serialize(writer, AnObject, emptyNamespace);
MessageBox.Show(wr.ToString());
}
First of all, you should stop using new XmlTextReader(). That has been deprecated since .NET 2.0. Use XmlReader.Create() instead.
Second, XML is not a random-access medium. You can't move forward and backwards, writing into the middle of the file. It's a text-based file.
If you need to "modify" the file, then you'll need to write a new version of the file. You could read from the original file, up to the point where you need to deserialize, writing the nodes out to a new version of the file. You could then deserialize from the original file, modify the objects, and serialize out to the new version. You could then continue reading from the original and writing the nodes out to the new version.

How to visualize XAML

I have a task to write down a simple viewer that would show how a .xaml file visually looks like(just like the VS editor, but without editing capabilities). Could you give me any references that would help me?
you can use XamlReader.Load method
If you want only viewer, not a designer you can call XamlReader.Load method, and assign the result to ContentControl.Content
StringReader stringReader = new StringReader(strXaml);
XmlReader xmlReader = XmlReader.Create(stringReader);
myContentControl.Content = (Button)XamlReader.Load(xmlReader);
EDIT
This code loads window from xaml and show it
string strXaml = "<Window xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" Title=\"MainWindow\" Height=\"350\" Width=\"525\">" +
"<Grid> <Button Content=\"Button123\" Height=\"23\" HorizontalAlignment=\"Left\" Margin=\"174,41,0,0\" Name=\"button1\" VerticalAlignment=\"Top\" Width=\"75\" />"+
"</Grid></Window>";
StringReader stringReader = new StringReader(strXaml);
XmlReader xmlReader = XmlReader.Create(stringReader);
Window obj = (Window)XamlReader.Load(xmlReader);
obj.Show();
If you are sure that the root element is allways Window, you can skip it. Something like this works for me
StringReader stringReader = new StringReader(strXaml);
var xDoc = XDocument.Load(stringReader).Document.Descendants().First().DescendantNodes().First();
XmlReader xmlReader = xDoc.CreateReader();
uc.Content = XamlReader.Load(xmlReader);
Would be better to check if Window exists or not before skipping it
You should start by looking at the XamlServices class and its Parse method.

Compress XML during XMLReader.Read()

I have a method which loops through an XML document using an XMLReader (validating at the same time) extracting specific pieces of information. I also need to compress the entire XML document in preparation for storing it in a database. The code I have to do this is below. Is this (passing the entire XmlReader to StreamWriter.Write()) the appropriate / most efficient way to achieve this? I didn't see a clear way to use the while(validatingReader.Read()) loop to achieve the same result.
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.Add("schemaNamespace", "schemaLocation");
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.ValidationType = ValidationType.Schema;
readerSettings.Schemas.Add(schemaSet);
readerSettings.ValidationEventHandler
+= new ValidationEventHandler(XMLValidationError);
using (XmlReader documentReader = requestXML.CreateNavigator().ReadSubtree())
{
using (XmlReader validatingReader =
XmlReader.Create(documentReader, readerSettings))
{
using (MemoryStream output = new MemoryStream())
{
using (DeflateStream gzip =
new DeflateStream(output, CompressionMode.Compress))
{
using (StreamWriter writer =
new StreamWriter(gzip, System.Text.Encoding.UTF8))
{
writer.Write(validatingReader);
this.compressedXMLRequest
= Encoding.UTF8.GetString(output.ToArray());
}
}
}
while (validatingReader.Read())
{
// extract specific element contents
}
}
}
Compression portion looks fine. MemoryStream may not be the best choice for large documents, but check if performance is ok for your scenarios before changing.
"extract specific element" portion will not read anything as reader is forward only, so all content is already read by the time that portion is executed. You may want to recreate the reader.
For future reference:
The code in the Question does not work properly. Passing an XmlReader to a StreamWriter doesn't work as expected. In the end I didn't end up combining compression with validation in this way so I don't exactly have "correct" code to show for this but didn't want to leave the question dangling.

Categories

Resources