Unexpected XML declaration in C# and Linq to xml

  • Thread starter Thread starter Vahed Mamqaderi
  • Start date Start date
V

Vahed Mamqaderi

Guest
Hi

I want to open a xml entry in ZipArchive to editing it with Linq to XML.

This is my code :

using (FileStream fs = new FileStream(ProjectFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
// create or read a zip archive with ZipArchive.
using (ZipArchive archive = new ZipArchive(fs, ZipArchiveMode.Update))
{
var entry = archive.Entries.SingleOrDefault(e => e.FullName.Contains(bilinearUang.PushOver.ModelName));
//ZipArchiveEntry entry = archive.GetEntry(bilinearUang.PushOver.ModelName + ".xml");
if (entry == null)
throw new Exception($"Not found '{bilinearUang.PushOver.ModelName + ".xml"}' ");
Stream entryStream = entry.Open();
entryStream.Position = 0;
XElement xEl = XElement.Load(entryStream);
var vs = xEl.Descendants(nameof(bilinearUang.Vs)).SingleOrDefault();
var ds = xEl.Descendants(nameof(bilinearUang.Delta_s)).SingleOrDefault();

if (vs != null) vs.Value = bilinearUang.Vs.ToString(CultureInfo.InvariantCulture);
if (ds != null) ds.Value = bilinearUang.Vs.ToString(CultureInfo.InvariantCulture);

xEl.Save(entryStream);
entryStream.Dispose();
}
}

but it's throw in exception:

System.Xml.XmlException: 'Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it. Line 414, position 14.'

1360334.png

Continue reading...
 
Back
Top