Skip to content
This repository was archived by the owner on Feb 15, 2021. It is now read-only.
This repository was archived by the owner on Feb 15, 2021. It is now read-only.

XElement.ToJson + Json.ToXElement don't round-trip #289

@SteveGilham

Description

@SteveGilham

Although it looks like it was intended to, with the adding of the "-" sigil to distinguish attribute names in the resulting JSON, exporting XML as JSON text and then re-importing throws,

To Reproduce
Run this

using System;
using System.Xml.Linq;

using Manatee.Json;

namespace Example
{
  internal class Program
  {
    private static void Main(string[] args)
    {
      var xmltext = "<Items><Item k=\"1\" v=\"a\" /><Item k=\"2\" v=\"b\" /></Items>";
      var xml = XDocument.Parse(xmltext);

      var jsontext = XmlExtensions.ToJson(xml.Root).ToString();

      var json = JsonValue.Parse(jsontext);

      var xmlout = json.ToXElement("dummy");

      Console.WriteLine("{0}", xmlout);
    }
  }
}

Expected behavior
A default formatted version of the input xmltext value printed out.

Actual behaviour
Raises

System.Xml.XmlException
  HResult=0x80131940
  Message=Name cannot begin with the '-' character, hexadecimal value 0x2D.
  Source=System.Private.Xml
  StackTrace:
   at System.Xml.XmlConvert.VerifyNCName(String name, ExceptionType exceptionType) in /_/src/System.Private.Xml/src/System/Xml/XmlConvert.cs:line 417
   at System.Xml.XmlConvert.VerifyNCName(String name) in /_/src/System.Private.Xml/src/System/Xml/XmlConvert.cs:line 406
   at System.Xml.Linq.XName..ctor(XNamespace ns, String localName) in /_/src/System.Private.Xml.Linq/src/System/Xml/Linq/XName.cs:line 26
   at System.Xml.Linq.XNamespace.GetName(String localName, Int32 index, Int32 count) in /_/src/System.Private.Xml.Linq/src/System/Xml/Linq/XNamespace.cs:line 223
   at System.Xml.Linq.XNamespace.GetName(String localName) in /_/src/System.Private.Xml.Linq/src/System/Xml/Linq/XNamespace.cs:line 59
   at System.Xml.Linq.XName.Get(String expandedName) in /_/src/System.Private.Xml.Linq/src/System/Xml/Linq/XName.cs:line 85
   at System.Xml.Linq.XName.op_Implicit(String expandedName) in /_/src/System.Private.Xml.Linq/src/System/Xml/Linq/XName.cs:line 108
   at Manatee.Json.XmlExtensions._GetXName(String key)
   at Manatee.Json.XmlExtensions.ToXElement(JsonValue json, String key)
   at Manatee.Json.XmlExtensions.ToXElement(JsonValue json, String key)
   at Manatee.Json.XmlExtensions.ToXElement(JsonValue json, String key)
   at Manatee.Json.XmlExtensions.ToXElement(JsonValue json, String key)
   at Manatee.Json.XmlExtensions.ToXElement(JsonValue json, String key)
   at Example.Program.Main(String[] args) in C:\Users\steve\source\repos\ClassLibrary1\ConsoleApp6\Program.cs:line 19

Desktop (please complete the following information):

  • OS: Win10 Home
  • .Net Target: netcoreapp3.1
  • Version: 13.0.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions