Visual studio indent string - c#

having this string
var commandAfterSerialize = $#"{{""LotId"":""00000000-0000-0000-0000-000000000000"",""AuctionId"":""00000000-0000-0000-0000-000000000000"",""DisableTotalSpendUpdate"":false,""AltInternetSurchargeRate"":null,""WinningBidders"":{{""{winningBidderKey}"":true,""{winningBidderKeySecond}"":false}},""Timestamp"":{timeStampUtc.Ticks},""TimestampUtc"":""{timeStampUtc:o}""}}";
is it possible to display it in better way in visual studio (for editing purpose) without modifing string itself ? without inserting \r\n into it ?
better formating like json
I am using visual studio 2022, c# 6.0

C# 11
Allows you to use raw string literals
Example taken from the linked resource:
var xml = """
<element attr="content">
<body>
</body>
</element>
""";
Older C# versions
You can do something like this:
var xml = "<element attr=\"content\">"+
" <body>"+
" </body>"+
"</element>";
Nevermind the XML, this works of course with any string content.
Outside these, I'd like to second #DavidG's comment:
If you want to embed larger, more complex JSON in your app, better to put them in a resource file
Which has further advantages, as for example you could validate the json. You can have json-specific editor behavior, etc.

Related

Invalid characters in XML values [duplicate]

Currently, I'm working on a feature that involves parsing XML that we receive from another product. I decided to run some tests against some actual customer data, and it looks like the other product is allowing input from users that should be considered invalid. Anyways, I still have to try and figure out a way to parse it. We're using javax.xml.parsers.DocumentBuilder and I'm getting an error on input that looks like the following.
<xml>
...
<description>Example:Description:<THIS-IS-PART-OF-DESCRIPTION></description>
...
</xml>
As you can tell, the description has what appears to be an invalid tag inside of it (<THIS-IS-PART-OF-DESCRIPTION>). Now, this description tag is known to be a leaf tag and shouldn't have any nested tags inside of it. Regardless, this is still an issue and yields an exception on DocumentBuilder.parse(...)
I know this is invalid XML, but it's predictably invalid. Any ideas on a way to parse such input?
That "XML" is worse than invalid – it's not well-formed; see Well Formed vs Valid XML.
An informal assessment of the predictability of the transgressions does not help. That textual data is not XML. No conformant XML tools or libraries can help you process it.
Options, most desirable first:
Have the provider fix the problem on their end. Demand well-formed XML. (Technically the phrase well-formed XML is redundant but may be useful for emphasis.)
Use a tolerant markup parser to cleanup the problem ahead of parsing as XML:
Standalone: xmlstarlet has robust recovering and repair capabilities credit: RomanPerekhrest
xmlstarlet fo -o -R -H -D bad.xml 2>/dev/null
Standalone and C/C++: HTML Tidy works with XML too. Taggle is a port of TagSoup to C++.
Python: Beautiful Soup is Python-based. See notes in the Differences between parsers section. See also answers to this question for more
suggestions for dealing with not-well-formed markup in Python,
including especially lxml's recover=True option.
See also this answer for how to use codecs.EncodedFile() to cleanup illegal characters.
Java: TagSoup and JSoup focus on HTML. FilterInputStream can be used for preprocessing cleanup.
.NET:
XmlReaderSettings.CheckCharacters can
be disabled to get past illegal XML character problems.
#jdweng notes that XmlReaderSettings.ConformanceLevel can be set to
ConformanceLevel.Fragment so that XmlReader can read XML Well-Formed Parsed Entities lacking a root element.
#jdweng also reports that XmlReader.ReadToFollowing() can sometimes
be used to work-around XML syntactical issues, but note
rule-breaking warning in #3 below.
Microsoft.Language.Xml.XMLParser is said to be “error-tolerant”.
Go: Set Decoder.Strict to false as shown in this example by #chuckx.
PHP: See DOMDocument::$recover and libxml_use_internal_errors(true). See nice example here.
Ruby: Nokogiri supports “Gentle Well-Formedness”.
R: See htmlTreeParse() for fault-tolerant markup parsing in R.
Perl: See XML::Liberal, a "super liberal XML parser that parses broken XML."
Process the data as text manually using a text editor or
programmatically using character/string functions. Doing this
programmatically can range from tricky to impossible as
what appears to be
predictable often is not -- rule breaking is rarely bound by rules.
For invalid character errors, use regex to remove/replace invalid characters:
PHP: preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $s);
Ruby: string.tr("^\u{0009}\u{000a}\u{000d}\u{0020}-\u{D7FF}\u{E000‌​}-\u{FFFD}", ' ')
JavaScript: inputStr.replace(/[^\x09\x0A\x0D\x20-\xFF\x85\xA0-\uD7FF\uE000-\uFDCF\uFDE0-\uFFFD]/gm, '')
For ampersands, use regex to replace matches with &: credit: blhsin, demo
&(?!(?:#\d+|#x[0-9a-f]+|\w+);)
Note that the above regular expressions won't take comments or CDATA
sections into account.
A standard XML parser will NEVER accept invalid XML, by design.
Your only option is to pre-process the input to remove the "predictably invalid" content, or wrap it in CDATA, prior to parsing it.
The accepted answer is good advice, and contains very useful links.
I'd like to add that this, and many other cases of not-wellformed and/or DTD-invalid XML can be repaired using SGML, the ISO-standardized superset of HTML and XML. In your case, what works is to declare the bogus THIS-IS-PART-OF-DESCRIPTION element as SGML empty element and then use eg. the osx program (part of the OpenSP/OpenJade SGML package) to convert it to XML. For example, if you supply the following to osx
<!DOCTYPE xml [
<!ELEMENT xml - - ANY>
<!ELEMENT description - - ANY>
<!ELEMENT THIS-IS-PART-OF-DESCRIPTION - - EMPTY>
]>
<xml>
<description>blah blah
<THIS-IS-PART-OF-DESCRIPTION>
</description>
</xml>
it will output well-formed XML for further processing with the XML tools of your choice.
Note, however, that your example snippet has another problem in that element names starting with the letters xml or XML or Xml etc. are reserved in XML, and won't be accepted by conforming XML parsers.
IMO these cases should be solved by using JSoup.
Below is a not-really answer for this specific case, but found this on the web (thanks to inuyasha82 on Coderwall). This code bit did inspire me for another similar problem while dealing with malformed XMLs, so I share it here.
Please do not edit what is below, as it is as it on the original website.
The XML format, requires to be valid a unique root element declared in the document.
So for example a valid xml is:
<root>
<element>...</element>
<element>...</element>
</root>
But if you have a document like:
<element>...</element>
<element>...</element>
<element>...</element>
<element>...</element>
This will be considered a malformed XML, so many xml parsers just throw an Exception complaining about no root element. Etc.
In this example there is a solution on how to solve that problem and succesfully parse the malformed xml above.
Basically what we will do is to add programmatically a root element.
So first of all you have to open the resource that contains your "malformed" xml (i. e. a file):
File file = new File(pathtofile);
Then open a FileInputStream:
FileInputStream fis = new FileInputStream(file);
If we try to parse this stream with any XML library at that point we will raise the malformed document Exception.
Now we create a list of InputStream objects with three lements:
A ByteIputStream element that contains the string: <root>
Our FileInputStream
A ByteInputStream with the string: </root>
So the code is:
List<InputStream> streams =
Arrays.asList(
new ByteArrayInputStream("<root>".getBytes()),
fis,
new ByteArrayInputStream("</root>".getBytes()));
Now using a SequenceInputStream, we create a container for the List created above:
InputStream cntr =
new SequenceInputStream(Collections.enumeration(str));
Now we can use any XML Parser library, on the cntr, and it will be parsed without any problem. (Checked with Stax library);

In C# Using System.Xml.Linq XText how do I get an & in the document instead of &

Using MS Visual Studio 2013 to create a C# application, I am trying to get the following output in an XML document.
<UnitsOfMeasure>
&uom-data;
</UnitsOfMeasure>
I keep getting
<UnitsOfMeasure>
&uom-data;
</UnitsOfMeasure>
Here is the code I have tried
XElement uom = new XElement("UnitsOfMeasure");
uom.Add("\n" + tab2, new XText("&uom-data;"), "\n" + tab1);
sd.Add("\n" + tab1, uom);
sd.Add("\n");
XElement uom = new XElement("UnitsOfMeasure");
uom.Add("\n" + tab2, new XText((char)38 + "uom-data;"), "\n" + tab1);
sd.Add("\n" + tab1, uom);
sd.Add("\n");
Thanks
The problem is that & has a special meaning in XML - it's used to escape other things; see beware of the ampersand when using xml, for example. What's being written for you is the correct way to include an ampersand inside XML and when an XML parser reads it back in, it should convert the & back to &.
So perhaps, if anything, you may have a problem with whatever code is reading that XML back in again as it should be converting it back for you.
XML has things called "entities", which take the form ampersand-characters-semicolon.
The XML entity is a alias for a different block of text (although in most cases, entities are just used just to insert a single character -- generally characters not on the keyboard)
& is the most commonly used -- it's to insert an &. © is for the copyright symbol.
In addition to the standard ones, you are allowed to define your own.
The fact that what you are trying to enter -- &uom-data; -- so neatly follows the entity format, I suspect that it really IS an entity and you are just missing the part where it's defined.

Concatenating XML Element Values to String using XDocument

Given an XML Document - for instance:
<factory>
<widgets>
<widget>Foo</widget>
<widget>Bar</widget>
<widget>Baz</widget>
<widget>Qux</widget>
</widgets>
</factory>
I wish to build a line-break separated string of widget values - using the above XML, this would be:
Foo
Bar
Baz
Qux
The code I'm using to do this is:
var doc = XDocument.Parse(xml) //where XML is a string containing the above XML
var builder = new StringBuilder();
foreach(var widget in doc.Root.Element("widgets").Elements("widget"))
{
builder.AppendLine(widget.Value);
}
However, the resulting string is FooBarBazQux rather than a newline-separated version. Setting a breakpoint on the AppendLine call reveals that widget.Value is being set to "FooBarBazQux", and the loop runs once rather than the 4 times I'm expecting.
I've tried running the query in LinqPad:
XDocument settings = XDocument.Parse (#"
<factory>
<widgets>
<widget>Foo</widget>
<widget>Bar</widget>
<widget>Baz</widget>
<widget>Qux</widget>
</widgets>
</factory>");
foreach(var x in settings.Root.Elements("widgets").Elements("widget"))
x.Value.Dump("Widget Type");
and the results are correct and as expected.
Can anyone help me in getting a newline separated string of Widget values? I'm at a bit of a loss!
It might be worth noting that this is within a Xamarin.Forms application, using the PCL version of using System.Xml.Linq.
I don't know whether I should add it as an answer so it can be marked as such (or if I will get shouted at because I have already put it in a comment)
builder.ToString()
This was a stupid mistake, caused by "Fast Deployment" being enabled in the Android Xamarin project.
My XML File was being generated by the app if it didn't exist - I assumed that on re-deploying, all app assets were to being removed from the device. It turns out that when Fast Deployment is enabled, the device only updates NEW components of the application - i.e. the XML file wasn't being removed. The problem was caused by the XML file being out of date.
I believe that the XML document being initially incorrect was caused by the potential issue that StuartLC pointed out - I must have at one point called builder.AppendLine(doc.Root.Element("widgets").Value), which returns the concatenation of the value of all child elements of widgets.
Thanks for all the help!

converting Xml to txt

I am currently working with an XML file that keeps race information in XML format like so
<Row xmlns="Practice2a">
<RecordType>Qualifying Classification</RecordType>
<_x0030_02150Position>3</_x0030_02150Position>
<Class>250</Class>
<_x0030_02150MachineNo>11</_x0030_02150MachineNo>
<RiderName>Kevin James</RiderName>
<Machine>Honda</Machine>
<_x0030_02150ToDBehind>29.680</_x0030_02150ToDBehind>
<_x0030_02150BestLapSpeed>97.1415157615475</_x0030_02150BestLapSpeed>
<_x0030_02150ToDBestLapTime>5:32.274</_x0030_02150ToDBestLapTime>
<_x0030_02150BestOnLap>7</_x0030_02150BestOnLap>
</Row>
I want to create a plain txt file with just some of the information , I just want in kind off in a table format e.g
pos Name racetime and BestLaptime
I have attempted to remove the tags from the file and create a txt file so now I get
I create a line count to possibly use as delimiters for extracting the right fields.
139 Qualifying Classification
140 3
141 250
142 11
Driver Name: Machine Type: Kevin James
145 Honda
146 29.680
147 97.1415157615475
148 5:32.274
My code is getting quite out of hand and I am wondering if there is a much better way to achieve this rather than adding 14 to count each time , that's how i am displaying Driver Name:" instead of a number.
Any pointers as to how you would go about this would be a great insight.
A quick solution would be to read your xml in XmlDocument (or even simpler to a dataset), and generate the text file in your c# code.
See:
Walkthrough: Reading XML Data into a Dataset
Read XML Attribute using XmlDocument
Alternate approach would be to define an xslt to reformat your xml to layout of your choice. Normally its a preferred approach for generating html docs from your xml datam, though could be used to transform into normal text reports. You can read more about it on
W3School- XSLT
XSLT Basics
You can parse and format it using LinqToXml:
using System.Xml.Linq
// [...]
// Load the XML, either from a string or from an url
var doc = XDocument.Parse(xmlString);
// or
var doc = XDocument.Load(new Uri(#"C:\myFile.xml"));
var result = String.Empty;
foreach (var el in doc.Descendants())
{
// do something with it and format the data to your liking... e.g.
result += FormatElement(el);
}
// or more compact
doc.Descendants().ToList().ForEach(el => result += FormatElement(el));
// [...]
private string FormatElement(XElement el)
{
return String.Format("{0}: {1}", el.Name, el.Value);
}
Of course you need to adapt the FormatElement method to your needs, but this scheme should work.
XML is designed so that some features are required and may be depended-on, while other things are the choice of the author of the document. Your scheme seems to get those features exactly backwards! Which line something appears on is not guaranteed by the standard. An entire legal XML file may occupy a single line.
The whole point of XML is that the use of a standard format allows for the use of common tools. The .NET Framework has (several) XML parsing components built in to it that can read this file and give you exactly the information you are looking for. You can then output that information as text in whatever format you like.
There is no reason to parse it yourself.
And remember, if your solution includes RegEx, then you've already lost.
(I'm kidding about that last part. Sort of.)

Is there a better Regex for parsing DTD

I've got the DTD for OFX 1.03 (their latest version despite having developed and released 1.60, but I digress...)
I would like to use regex to have groups that split an entity, element, other tags into its parts for further processing such that I would take a tag like this:
<!ENTITY % ACCTTOMACRO "(BANKACCTTO | CCACCTTO | INVACCTTO)">
And create an object like this
new EntityTag { string Name = "%ACCTTOMACRO"; string[] ChildTypes = new string[] {"BANKACCTTO", "CCACCTTO", "INVACCTTO"}};
I've got a regular expression that looks like this:
Regex re = new Regex(#"<!(\b)+([\s\S])?[^>]+>");
Admittedly, I'm new to regex, so I've done good so far getting this which gives me a match collection over the DTD for each tag without comments.
I would like to leverage grouping to facilitate creation of the previously mentioned object.
If I'm on the totally wrong path, please instruct me, however if you do download this document, I think you may find its not standard. (Visual studio throws up some red flags with the way this document is formatted)
I don't expect anyone to go to the trouble, but for the curious here is the link to download the specs.
It looks like they've got schema available as well. Why not download the schema instead and parse that with an XML parser (for instance, LINQ-to-XML)?

Categories