Linq query not working on XML string - xml-parsing

i have a web service which returns xml data as string. i am trying to apply linq on xml string which is retunred by the service.
Lets say the xml stirng is some thing like.
string str = "<root xmlns=\"http://tempuri.org/Count.xsd\"> <child> <subchild1>1</subchild1><subchild2>1</subchild2><subchild3>1</subchild3></child></root>";
Below is the c# code i am using.
XDocument xdoc = XDocument.Parse(str);
var item = xdoc.Element("root").Element("child").Element("subchild1");
but the above query is always returning null.
can any one correct me what is wrong in above peace of code.

This solved my problem.
XNamespace xnp = xdoc.Root.GetDefaultNamespace();
var item = xdoc.Element(xnp + "root").Element(xnp + "child").Element(xnp + "subchild1").Value;

Related

java swagger 3 annotations #ExampleObject from jsonfile

I'm documention one of my api with multiple examples like this:
#Operation(summary = "Create new")
#PostMapping("")
public ResponseEntity<Object> createOne(
#Parameter(description = "MyDto")
#io.swagger.v3.oas.annotations.parameters.RequestBody(
content = #Content(examples = {
#ExampleObject(name = "one", value = EXAMPLE_ONE),
#ExampleObject(name = "two", value = EXAMPLE_TWO),
#ExampleObject(name = "three", value = EXAMPLE_THREE)}
))
#RequestBody MyDTO body
) {
...
}
This works fine, though EXAMPLE_ONE is a string value. This is pretty unclear as you can see from the example below
private static final String EXAMPLE_ONE = "{\"glossary\":{\"title\":\"example glossary\",\"GlossDiv\":{\"title\":\"S\",\"GlossList\":{\"GlossEntry\":{\"ID\":\"SGML\",\"SortAs\":\"SGML\",\"GlossTerm\":\"Standard Generalized Markup Language\",\"Acronym\":\"SGML\",\"Abbrev\":\"ISO 8879:1986\",\"GlossDef\":{\"para\":\"A meta-markup language, used to create markup languages such as DocBook.\",\"GlossSeeAlso\":[\"GML\",\"XML\"]},\"GlossSee\":\"markup\"}}}}}";
I looking for a better way to provide the example. A json file would be nice, but I couldn't find anything about that.
You can use externalValue instead of value. See here
Use java text-block instead of normal quoted string
e.g. putting triple double (""")
see https://www.baeldung.com/java-text-blocks

Can't serialize records, in F#, with json.net

A very basic example:
type private test =
{
a : string
b : string list
}
let t = { a = "hello"; b = ["1"; "2"] }
let s = JsonConvert.SerializeObject(t)
This will produce an empty string.
I have seen that json.net supports F# and that there are a lot of posts related to enum types, etc but I'm not there yet: I'm trying to serialize something very simple.
Many posts point toward another json serializer project, called Chiron, but it was updated a year ago and they're still like:
We’re working on Guides and reference content for working with Chiron, so keep an eye on the Updates
Is there something obvious I haven't seen?
So ideally, working with json.net would be better, especially since I'm used to it in C#
The issue seems to be that Json.Net only serializes public fields of F# records. When you mark the record as private, all its fields also become private and those are ignored. The following works as expected for me:
type test =
{
a : string
b : string list
}
let t = { a = "hello"; b = ["1"; "2"] }
let s = JsonConvert.SerializeObject(t)
This produces the expected JSON:
{"a":"hello","b":["1","2"]}

Convert datasource guid to string value in sitecore rendering

In sitecore, when getting the datasource from the renderingcontext, it returns a guid. Is there a means to convert this to the actual string value stored in the datasource field.
I want to run a "fast" query but need the path stored in the rendering context datasource instead of the guid.
Thanks,
If what you receive is a Guid, you can use
var idString = guid.ToString("B");
if what you receive is Sitecore.Data.ID, just use:
var idString = id.ToString();
The guid that you are getting is the Sitecore ID of the datasource item. You should be able to get it's path directly:
var dataSource = Sitecore.Context.Database.GetItem(RenderingContext.CurrentOrNull.Rendering.DataSource);
var dataSourcePath = dataSource.Paths.Path;

How does the Dart URI class QueryParameters handle Map values?

According to the documentation, it needs to follows the Form Post rules at: https://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4. When looking at that information it did not give me much to work with in terms of complex objects or maps.
Right now, If I have a list for example: Each item in the list needs to be stringified.
var params = {"list": [1,2,3]};
// needs to be stringed.
params["list"] = params["list"].map((item)=>item.toString()).toList();
Simple. Also all base items need to be a string as well
var params = {"number": 1, "boolean": true};
params = params.forEach((k,v)=> params[k].toString());
But how do we handle maps?
var params = {"map": {"a":1,"b":"foo","c":false,"d":[]}};
// ??
It seems that after testing in my app and in dart pad, you need to make sure everything is strings, so i am trying to come up with a way to effectively cover lists, maps, and maybe more complex objects for encoding.
var params = {};
params["list"] = [1,2,3];
params["number"] = 1;
params["boolean"] = true;
params["map"] = {"a":1,"b":"foo","c":false,"d":[]};
params.forEach((String key, dynamic value){
if(value is List){
params[key] = value.map((v)=>v.toString()).toList();
}else if(value is Map){
// ????
}else{
params[key] = value.toString();
}
//maybe have an additional one for custom classes, but if they are being passed around they should already have their own JSON Parsing implementations.
}
Ideally, the result of this would be passed into:
Uri myUri = new Uri(queryParameters: params);
and right now, while i solved the list issue, it doesn't like receiving maps. Part of me just wanted to stringify the map as a whole, but i wasn't not sure if there was a better way. I know that when someone accidentally stringified the array, it was not giving me: ?id=1&id=2 but instead ?id=%5B1%2C2%5D which was not correct.
I don't think there is any special support for maps. Query parameters itself is a map from string to string or string to list-of-strings.
Everything else need to be brought into this format first before you can pass it as query parameter.
A simple approach would be to JSON encode the map and pass the resulting string as a single query parameter.

Sequence contains no elements - PayPal SOAP Response

I am trying to read the value from an XML element but I always get "Sequence contains to elements" error.
I already done my research but nothing works for my problem.
I want to read the Ack and Timestamp element values in this XML
<DoDirectPaymentResponse xmlns="urn:ebay:api:PayPalAPI">
<Timestamp xmlns="urn:ebay:apis:eBLBaseComponents">2014-09-16T04:41:56Z</Timestamp>
<Ack xmlns="urn:ebay:apis:eBLBaseComponents">Success</Ack>
</DoDirectPaymentResponse>
Here's my code for reading the Ack and Timestamp values
String xmlString = #xml;
using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
{
XDocument xdoc = XDocument.Load(reader);
var timestamp = xdoc.Descendants("Timestamp").Single();
receipt.Timestamp = timestamp.Value;
var response = xdoc.Descendants("Ack").Single();
receipt.Response = response.Value;
}
Please help me with this. Thanks a lot.
You need to use proper XNamespace to access elements in the namespace :
XDocument xdoc = XDocument.Parse(xmlString);
XNamespace ns = "urn:ebay:apis:eBLBaseComponents";
var timestamp = xdoc.Descendants(ns+"Timestamp").Single();
receipt.Timestamp = timestamp.Value;
var response = xdoc.Descendants(ns+"Ack").Single();
receipt.Response = response.Value;
Side note: you can use XDocument.Parse() to load XML from XML string content.

Resources