XDocument write Duplicated xml

2019-02-16 03:47发布

问题:

I have a method which load XML to a XDocument and modify its elements then save. But when I reload it. I got this error :

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.

I checking the XML and see that the XDocument didn't save the changed but create a duplicate and save.

It save the old one and the new one like this example xml :

<?xml version="1.0" encoding="UTF-8"?>
<Ungdungs>
  <Ungdung>
    <Name>HERE City Lens</Name>
    <Id>b0a0ac22-cf9e-45ba-8120-815450e2fd71</Id>
    <Path>/Icon/herecitylens.png</Path>
    <Version>Unknown</Version>
    <Category>HERE</Category>
    <Date>Uknown</Date>
  </Ungdung>
<?xml version="1.0" encoding="UTF-8"?>
<Ungdungs>
  <Ungdung>
    <Name>HERE City Lens</Name>
    <Id>b0a0ac22-cf9e-45ba-8120-815450e2fd71</Id>
    <Path>/Icon/herecitylens.png</Path>
    <Version>1.0.0.0</Version>
    <Category>HERE</Category>
    <Date>Uknown</Date>
  </Ungdung>

Here the code I used to modify and save XML :

using (Stream stream = storage.OpenFile("APPSDATA.xml", FileMode.Open, FileAccess.ReadWrite))
                {
                    //var xdoc = XDocument.Load("APPSDATA.xml");
                    var xdoc = XDocument.Load(stream, LoadOptions.None);
                    var listapp = from c in xdoc.Descendants("Ungdung") select c;

                    foreach (XElement app in listapp)
                    {
                        var xElement = app.Element("Name");
                        if (xElement != null)
                            progressIndicator.Text = "Checking " + xElement.Value + "...";
                        var element = app.Element("Id");
                        if (element != null)
                        {
                            var appId = element.Value;
                            var appVersion = await GetAppsVersion(appId);
                            app.Element("Version").Value = appVersion.ToString();
                        }
                    }

                    xdoc.Save(stream);
                }

How can I solve this problem ?

回答1:

Looks like you're appending modified document at the end of current file content. That's why you can't parse it later again.

I would split read and write parts into different using statements:

XDocument xdoc;

using (Stream stream = storage.OpenFile("APPSDATA.xml", FileMode.Open, FileAccess.Read))
{
    xdoc = XDocument.Load(stream, LoadOptions.None);
}

var listapp = from c in xdoc.Descendants("Ungdung") select c;

foreach (XElement app in listapp)
{
    var xElement = app.Element("Name");
    if (xElement != null)
        progressIndicator.Text = "Checking " + xElement.Value + "...";
    var element = app.Element("Id");
    if (element != null)
    {
        var appId = element.Value;
        var appVersion = await GetAppsVersion(appId);
        app.Element("Version").Value = appVersion.ToString();
    }
}

using (Stream stream = storage.OpenFile("APPSDATA.xml", FileMode.Truncate, FileAccess.Write))
{
    xdoc.Save(stream);
}

Setting FileMode.Truncate on second using statement will clear previous file content, what should fix your problem.