0

I have used this Code all throughout my project and now that there has been an update it has stopped working. I cant find anything online this is the format I'm trying to get my XML

   <?xml version="1.0" encoding="utf-8" ?>
    <CATALOG>
      <ITEM>
        <UIElementIndex>GRID</UIElementIndex>
        <Left>0</Left>
        <Top>0</Top>
        <Width>595</Width>
        <Height>60</Height>
        <Name>Header</Name>
        <PlaceHolder></PlaceHolder>
        <R>170</R>
        <G>170</G>
        <B>170</B>
      </ITEM>
    </CATALOG>

I have been using this code, am i doing something Wrong

    using (var stream = await PDFCRFileReader.Instance.MainFile.OpenStreamForWriteAsync())
    {
        foreach (PDFCRElement element in PDFCRFileReader.Instance.documentElements)
        {
            XDocument document = XDocument.Load(stream);

            XElement xmlElemnt = new XElement("ITEM",
            new XAttribute("ID", "NIT " + element.Name + ((element.Left * element.Top) * element.Width).ToString()),
            new XElement("UIElementIndex", element.UIElementIndex),
            new XElement("Left", element.Left),
            new XElement("Top", element.Top),
            new XElement("Width", element.Width),
            new XElement("Height", element.Height),
            new XElement("Name", element.Name),
            new XElement("PlaceholderText", element.PlaceholderText),
            new XElement("R", element.R),
            new XElement("B", element.B),
            new XElement("G", element.G));

            document.Root.Element("CATALOG").Add(xmlElemnt);

            document.Save(stream);
            var dialog = new MessageDialog("FIle Saved!", "FILE SAVE STATUS");
            await dialog.ShowAsync();
        }
        stream.Flush();
    }

I am getting the error, it makes no sense that this was working yesterday

System.NullReferenceException: 'Object reference not set to an instance of an object.'

  • Aside from anything, saving multiple XML documents to the same stream is almost certainly a bad idea. Are you certain you don't want to save all the elements in *one* document? – Jon Skeet Dec 04 '17 at 07:02
  • (I'd also strongly advise you to use indentation to make it clear where statements start and end. You have one big statement in the middle of that loop, but your indentation makes it looks like it's a lot of separate statements.) – Jon Skeet Dec 04 '17 at 07:03

1 Answers1

0

The problem is because the Root element "CATALOG" cannot be find which raised this issue. I used the following code then find that the result is null:

    var root = document.Root;
    var cataroot = root.Element("CATALOG");

To solve this from your own code, use the following directly:

    document.Root.Add(xmlElemnt);

And maybe you can report/consult it to here. Maybe the rule has been changed in this API. By the way, I think the change is reasonable. Here the root is already the CATALOG, root.Element("CATALOG") is trying to find the CATALOG node from the root.

Barry Wang
  • 1,354
  • 1
  • 5
  • 12