1

I am generating an XML file using C# and I need to place multiple values inside a single attribute, attribute values are not supposed to be in a string outside the attribute. My group leader has requested it to be more read-able by adding line breaks after each value. I am quite struggling with figuring out how to do this.

The results have to be placed within a single attribute, creating multiple attributes or attribute + string is not an option.

What I have tried so far:

  • \n\r gives an "Invalid XML NCName" error;

  • &#10&#13 gives an "Invalid XML NCName" error;

  • I have not managed to find a different class than XDocument in C# which allows me to nicely place multiple variables in a single attribute and that supports line breaks.

Code is simplified but this is the idea of the workflow.

XDocument doc = new XDocument(
                  new XDeclaration("1.0", "utf-8", "yes"),
                  new XComment(" XML File "),
                );

new XElement("Source1",
                new XAttribute("id", SourceID[i]),
                new XAttribute("active", SourceAct[i])
                new XAttribute("gainDB", SourceGain[i]);

new XElement("Audio",
            new XAttribute("id", IDSorted[i]),
            new XAttribute("file", PathList[i])
        )

doc.Save(@"XMLFiles/" + Description + ".xml");

What it prints:

<Source1 id="src:BlueSphere" gainDB="3.4" active="True" />
<AudioStream id="signal:violine" file="Assets/Audio/myaudio.wav" />`

What I need:

<Source1 id="src:BlueSphere" 
         gainDB="3.4" 
         active="True"/>
<AudioStream id="signal:violine" 
         file="Assets/Audio/myaudio.wav" />`

As I mentioned before, the code is simplified and in real life I have a lot more values within the attribute so it gets clogged up quite quickly.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • Why do you need that exactly? Just to be more readable? In that case why not use a text viewer with proper syntax highlighting instead.... – Igor Feb 14 '19 at 17:34
  • Well, to align them like you want, you'd have to know your indent level and a bunch of other things, keep track of it, add the appropriate amount of whitespace, add carriage returns and line feeds. All so it's more "readable". Consider asking your group leader to use an IDE capable of formatting the XML to his liking. – Heretic Monkey Feb 14 '19 at 17:35
  • See answer https://stackoverflow.com/a/14449850/1260204 in the marked duplicate. You will want to use [XmlWriterSettings](https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmlwritersettings?view=netframework-4.7.2) and the properties `NewLineOnAttributes`, `NewLineChars`, `IndentChars`. You can probably get close to what was asked but I doubt you can do it exactly the way that he/she wants. – Igor Feb 14 '19 at 17:42
  • @Igor Notepad++ is used to view the XML file, it has syntax highlighting but please bare in mind that each element has about 10 attributes in it now and later on it will be a lot more so it just takes up a lot of horizontal scrolling. Also XDocument, class that I am using from C#, seems to ignore XmlWriterSettings I feel like the solution I am looking for is an efficient information transfer from information saved via XDocument class to another class like XmlTextWriter or XmlWriter but I am not sure how to approach this – Jellyracecar Feb 14 '19 at 17:49
  • `Also XDocument, class that I am using from C#, seems to ignore XmlWriterSettings` <= see the [answer](https://stackoverflow.com/a/14449850/1260204) I mentioned above in my last comment. It has a whole code fragment you can almost copy/paste and use. – Igor Feb 14 '19 at 17:52

1 Answers1

0

Try using &#10;&#13; in the attribute instead of #&10#&13

Jay Buckman
  • 581
  • 5
  • 18