question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

XML Encoding Not Working

See original GitHub issue

I am using a symbol to replace content in a csproj file. I understand from https://github.com/dotnet/templating/issues/368 that I can XML encode the contents like so:

  "symbols": {
    "Title": {
      "type": "parameter",
      "datatype": "string",
      "defaultValue": "Project Title",
      "replaces": "PROJECT-TITLE",
      "description": "The name of the project which determines the assembly product name.",
      "forms": [ "encode" ]
    },
  "forms": {
    "encode": {
      "identifier": "xmlEncode"
    }
  }

When I run:

dotnet new graphql --name Foo --Title "Foo & Bar"

My csproj file is output with:

<Product>Foo & Bar</Product>

XML encoding is failing to occur. As an aside, what happens if you want to make the replacement in a csproj and separately a json file? i.e. I only want to XML encode when I replace the content in the csproj file and not the JSON file.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
seancpeterscommented, May 1, 2018

I agree, it is a bit complicated. Here’s another way to do it that you might like better, and doesn’t require sometimes xml encoding in the template source. Not too long ago, we introduced “derived symbols” which allow transforming the value of one symbol into the value of another. So a template could be setup to have a title symbol as a normal string parameter, such as:

    "Title": {
	  "type": "parameter",
	  "datatype": "string",
	  "defaultValue": "Project Title",
	  "replaces": "PROJECT TITLE",
	  "description": "Plaintext title",
	},

And then define a symbol whose value is derived from this symbol, and replaces something else, such as:

    "XmlEncodedTitle": {
	  "type": "derived",
	  "replaces": "PROJECT XML TITLE",
	  "description": "Xml encoded title",
	  "valueSource": "Title",
	  "valueTransform": "xmlEncode"
	}

The derived symbol’s value is determined by the attributes:

  • valueSource: The name of the other symbol that this symbol’s value is derived from.
  • valueTransform: How the other symbol’s value is transformed to generate this value. This can be a built in value form, or one defined in the “forms” section of the template.

Given the above symbol definitions, and template content of:

<Product>PROJECT XML TITLE</Product>
<Product>PROJECT TITLE</Product>

…and invoking the template with --Title "Foo & Bar", the content would be transformed to:

<Product>Foo &amp; Bar</Product>
<Product>Foo & Bar</Product>

With this method (if a jsonEncode value form existed), then for adding a json encoded version of the title, and appropriate replacements, we’d just define another derived symbol, such as:

    "JsonEncodedTitle": {
	  "type": "derived",
	  "replaces": "PROJECT JSON TITLE",
	  "description": "Json encoded title",
	  "valueSource": "Title",
	  "valueTransform": "jsonEncode"
	}

Note that there is no need to have the words “json” or “xml” in the replaces values, I was using those values for clarity.

0reactions
RehanSaeedcommented, May 2, 2018

BTW, I had to add a datatype property. The JSON schema needs updating as I was getting warnings about type being set to derived.

    "TitleXmlEncoded": {
      "type": "derived",
      "datatype": "string",
      "replaces": "PROJECT-TITLE-XML",
      "valueSource": "Title",
      "valueTransform": "xmlEncode"
    },
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Fix XML Files with Bad Encoding - FME Community
Fixing this problem means re-encoding the XML data with the proper encoding scheme. To do this, create an FME Workspace to convert the...
Read more >
XML encoding issue
A proper XML parser will fail to load an XML document if it contains any invalid characters - that is, byte sequences which...
Read more >
XML Encoding failing due Encoding not being UTF-8
The main reason for this error is that, the XML Parser cannot parse the XML since it contains non UTF-8 encoded characters. The...
Read more >
Error when trying to handle & character in XML message ...
I need to encode special characters like < and > and & and ' and " in a webMethods service. To test it...
Read more >
XML encoding and DOM interface methods
This article describes XML encoding and DOM interface methods. ... How to Encode XML Data addresses general XML encoding issues in detail: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found