Write XML Fragment with LINQ and Prefixes - namespaces

I have a document created in a constructor, and during execution I'm filling
it in with fragments generated from Custom Business Objects.
When I'm outputting the fragments, I need to include namespace fragments, but
I'd like to avoid adding the namespace url to each fragment, since it's defined in the root.
Any thoughts?
_doc = new XDocument(
new XDeclaration("1.0", "UTF-8", "yes"),
new XElement(aw + "kml",
new XAttribute(XNamespace.Xmlns + "gx", "http://www.google.com/kml/ext/2.2"),
new XAttribute("xmlns", "http://www.opengis.net/kml/2.2"),
new XElement(aw+"Document",
That's how the doc starts, so the namespaces are there.
How do I go about building an XElement to add using the gx prefix?

Use the same URI for an XNamespace:
XNamespace gx = "http://www.google.com/kml/ext/2.2";
XElement foo = new XElement(gx + "foo");
LINQ to XML will use the appropriate prefix automatically, as I understand it.

Related

View NotFound Exception in net core6

I wanna Render View To Stream.
this is my code:
var httpContext = new DefaultHttpContext { RequestServices = _serviceProvider };
var actionContext = new ActionContext(httpContext, new
RouteData(), new ActionDescriptor());
var viewEngineResult = _viewEngine.FindView(actionContext,
viewName, false);
this code worked before. but after update to core6 doesn't work.
net core 6 does not create view.dll
my razor view is in Views/Shared location. and i set it as content
Compiler no longer produces a Views assembly
The Razor compiler no longer produces a separate Views.dll file that
contains the CSHTML views defined in an application.
Both views and application types are included in a single
AppName.dll assembly. View types have the accessibility modifiers
internal and sealed, by default, and are included under the
AspNetCoreGeneratedDocument namespace.
So, If your method is doing something by View.dll, It is not work in .Net 6
Refer to this doc to learn more.

Details about the json:Array feature of Newtonsoft.JSON XML to JSON converter

Referencing this example of using "json:Array": Converting between JSON and XML
I have two questions:
Does the namespace have to be "json"? I.e. if ns2 matched back to
"xmlns:ns2='http://james.newtonking.com/projects/json'" would that work?
Can the namespace be omitted? Can I just put "Array='true'"?
I'm about to try to test by trial and error, but thought maybe somebody would know the answer, or someone in the future would like to know.
Not that it matters a whole lot, but my XML is being generated by BizTalk 2010 and I'm using a WCF CustomBehavior to call NewtonSoft as follows:
private static ConvertedJSON ConvertXMLToJSON(string xml)
{
// To convert an XML node contained in string xml into a JSON string
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
ConvertedJSON convertedJSON = new ConvertedJSON();
convertedJSON.JSONtext = JsonConvert.SerializeXmlNode(doc, Newtonsoft.Json.Formatting.None);
convertedJSON.rootElement = doc.DocumentElement.Name;
return convertedJSON;
}
Looks like the namespace has to be exactly what they provide:
string xmlToConvert2 = "<myRoot xmlns:json='http://james.newtonking.com/projects/json'><MyText json:Array='true'>This is the text here</MyText><Prices><SalesPrice>10.00</SalesPrice></Prices></myRoot>";
string strJSON2 = ConvertXMLToJSON(xmlToConvert2);
As with normal xml, the namespace prefix can be any value. The follow worked equally as well as the above.
string xmlToConvert3 = "<myRoot xmlns:abc='http://james.newtonking.com/projects/json'><MyText abc:Array='true'>This is the text here</MyText><Prices><SalesPrice>10.00</SalesPrice></Prices></myRoot>";
string strJSON3 = ConvertXMLToJSON(xmlToConvert3);

Parse a custom XML with as3

i have a XML and i didn't found a way how to parse it (read the data inside)
<Saa:Header>
<Saa:Message>
<Saa:SenderReference> data
</Saa:SenderReference>
</Saa:Message>
<Saa:Message>
<Saa:SenderReference> data
</Saa:SenderReference>
</Saa:Message>
</Saa:Header>
i use the as3 language
Going with your example in your question (which assumes your data is a string), you could do something along these lines:
//your xml needs a namespace identifier to be valid
//I've added one on the header node.
//Likely if you're consuming this xml from some third party, the namespace declaration will be on root node.
var myXML = <Saa:Header xmlns:Saa="urn:swift:saa:xsd:saa.2.0" >
<Saa:Message>
<Saa:SenderReference> data
</Saa:SenderReference>
</Saa:Message>
<Saa:Message>
<Saa:SenderReference> data
</Saa:SenderReference>
</Saa:Message>
</Saa:Header>;
//create a reference to the `Saa` namespace that prefixes all your xml nodes
var saaNamespace:Namespace = new Namespace("urn:swift:saa:xsd:saa.2.0");
//tell AS3 to use that namespace by default
default xml namespace = saaNamespace;
//basically an array of all the SenderReference nodes in the entire xml
var xmlList:XMLList = myXML..SenderReference;
//This line will give the same results as the line above, but if there were SenderReference nodes somewhere else in the document that weren't under Message nodes, they would not be included (unlike above)
xmlList = myXML.Message.SenderReference;
//iterate through all the SenderReference nodes
for(var i:int=0;i<xmlList.length();i++){
trace("Node #" + (i+1) + ":",xmlList[i]);
}
There are lots of ways to get the data you want from XML in AS3, a good article is this one from senocular.
The XML class is the way.
[it] contains methods and properties for working with XML objects. The XML class (along with the XMLList, Namespace, and QName classes) implements the powerful XML-handling standards defined in ECMAScript for XML (E4X) specification (ECMA-357 edition 2).

In WinRT assembly.GetManifestResourceNames() gives no ResourceNames

I am using to get the assembly for following
Assembly assembly = Application.Current.GetType().GetTypeInfo().Assembly;
and i get the ResourceNames form that assembly for using
var resources=assembly.GetManifestResourceNames()
but it gives no resource names in assembly.
Please help me how to achieve it?
In VB.NET, I do that:
Public Function LoadResourceStr(sResID As String) As String
Dim ctx As Windows.ApplicationModel.Resources.Core.ResourceContext = New Windows.ApplicationModel.Resources.Core.ResourceContext()
ctx.Languages = {Globalization.CultureInfo.CurrentUICulture.Name}
Dim rmap As Windows.ApplicationModel.Resources.Core.ResourceMap = Windows.ApplicationModel.Resources.Core.ResourceManager.Current.MainResourceMap.GetSubtree("Resources")
Return rmap.GetValue(sResID, ctx).ValueAsString
End Function
The MainResourceMap is a collection of all resources of application. Important: the files into directories of app (embedded compiled) need is configured to "Content", not "embedded resource". In this case, you can change string ID from "Resources" to "Files".
The sample above show the resource strings into xml files, to translate your app.

Is there a list of defined C# functions for userCSharp in XSLT?

I'm trying to debug a BizTalk map that has some custom XSLT in it that makes use of C#. I've found:
userCSharp:MathSubtract
userCSharp:MathAdd
userCSharp:StringSize
userCSharp:StringSubstring
and a few others but I'm finding it difficult to find some resources online defining all of the available predefined c# functions and their documentation.
The reason I ask is because it has a I have a "userCSharp:StringFind" which blows up saying StringFind() is an unknown XSLT function.
The xslt functions MathSubtract, MathAdd etc correspond to the predefined Functoids that your map uses (in the xmlns 'userCSharp').
Most of the functoids are just inline XSLT C# functions - BizTalk adds the C# script for the functoid at the bottom of the xslt when the map gets compiled. (I think some of the simple functoids can use xslt primitives as well). Your own script functoids will also be added to this block.
You can see what BizTalk is doing by compiling your assembly containing the maps, and then using the "Show all Files" command to look at the corresponding .btm.cs file to see what has been added.
BizBert site gives quite a good reference on the implementation of each of the functoids.
(The double "" escaping is because the XSLT is kept in a string constant)
private const string _strMap = #"<?xml version=""1.0"" encoding=""UTF-16""?>
<xsl:stylesheet xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"" xmlns:msxsl=""urn:schemas-microsoft-com:xslt""
...
xmlns:userCSharp=""http://schemas.microsoft.com/BizTalk/2003/userCSharp"">
and then a script CDATA block at the bottom
<msxsl:script language=""C#"" implements-prefix=""userCSharp""><![CDATA[
public bool IsNumeric(string val)
{
if (val == null)
{
return false;
}
double d = 0;
return Double.TryParse(val, System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out d);
}
public string MathAdd(string param0, string param1)
{
System.Collections.ArrayList listValues = new System.Collections.ArrayList();
... etc