umbraco.library get nodename but string utf-8 show wrong - c#

I get nodename like this:
string text = umbraco.presentation.nodeFactory.Node.GetCurrent().Name;
but it shows like this:
BETON KÖÅÂÂK
How can I solve it?

GetCurrent().Name should return a string
(http://our.umbraco.org/wiki/reference/api-cheatsheet/working-with-nodefactory)
So if your string looks like that either your encoding is not set to utf-8 or your App_Data\umbraco.config file is corrupt.
The NodeFactory methods are basically a facade in front of the umbraco.config file - so have a look in that file to see if it is corrupted. The umbraco.config file is an xml file so also check that the first line is:
<?xml version="1.0" encoding="utf-8"?>
Secondly check your web.config for the encoding:
<system.web>
<globalization requestEncoding="UTF-8" responseEncoding="UTF-8"/>
Thirdly check all your Umbraco \config*.config files (which are all xml files) and make sure that the first line is:
<?xml version="1.0" encoding="utf-8"?>

Related

How to comment out line by line of a XML file in C# with System.XML

I need to comment and uncomment a XML node with child nodes in a file using System.XML.
Starting XML:
<?xml version="1.0" encoding="utf-8"?>
<test>
<!--comment...-->
<childTest>
<childchildTest>5<childchildTest/>
</childTest>
</test>
Commenting the whole node wouldn't be a problem and easy to achieve like in this example. But my problem is that I've already got some comments inside the node and nested comments aren't allowed per XML rules.
That means I would have to comment out line by line of the XML file so I would not destroy the XML file structure with nested comments.
Desired output:
<?xml version="1.0" encoding="utf-8"?>
<!-- <test> -->
<!--comment...-->
<!-- <childTest> -->
<!-- <childchildTest>5<childchildTest/> -->
<!-- </childTest> -->
<!-- </test> -->
Is it possible to achieve this with System.XML or would I have to do this with regex for example?
Assuming you have this XML well-organised in a file (meaning each node is on its own line, as you presented) you could use this one-liner:
File.WriteAllLines("path to new XML file", File.ReadAllLines("path to XML file").Select(line => line.Trim().StartsWith("<!--") ? line : $"<!--{line}-->"));
This part line.Trim().StartWith("<!--") ? line : $"<!--{line}-->" means if line is a comment (starts with <!--) then don't comment it, otherwise, do it.
In my opinion, there is no framework method which provides this functionality.
You can read XML file lines and then create new files which has comments for every line as shown in below code.
// Create a string array with the lines of text
string[] lines = File.ReadAllLines(path-of-file);
// Write the string array to a new file named "ouput.xml".
using (StreamWriter outputFile = new StreamWriter(Path.Combine(mydocpath,"output.xml"))) {
foreach (string line in lines)
outputFile.WriteLine("<!--" + line + "-->");
}

How to get specific attributes from all xml files in directory?

I have a folder full of xml files. In these files many of them share a common attribute (Name), but have a secondary attribute that is different. I want to get a list of the unique entries based off reading these xml files. Below is an example of what the various xml files will contain.
File 1
<?xml version="1.0" encoding="UTF-8"?>
<results date="2013-12-29">
<A uniqueId="1234" Name="My-Machine"/>
<error number="555">
<description><![CDATA[House on Fire]]></description>
</error>
</results>
File 2
<?xml version="1.0" encoding="UTF-8"?>
<results date="2013-12-29">
<A uniqueId="1234" Name="My-Machine"/>
<error number="556">
<description><![CDATA[House in flood]]></description>
</error>
</results>
File 3
<?xml version="1.0" encoding="UTF-8"?>
<results date="2013-12-29">
<A uniqueId="1234" Name="My-Machine"/>
<error number="556">
<description><![CDATA[House in flood]]></description>
</error>
</results>
I need to be able to read all the files, add each Name and description to a list (or possibly array). Output from example would look like this:
Name="MyMachine", description="![CDATA[House is flooding]]";
Name="MyMachine", description="![CDATA[House on fire]]";
Name="MyMachine", description="![CDATA[House on fire]]";
It seems LINQ may be the best way to handle this since the files are very small in content.
Here is a way to read that description element content from one file:
var xDoc = XDocument.Load("Input.xml");
var name = "My-Machine";
var aElement = xDoc.Root.Element("A");
string description = null;
if ((string)aElement.Attribute("Name") == name)
description = (string)xDoc.Root.Element("error").Element("description");
It will return the element value when Name attribute value matches your name variable. Otherwise description will be null.

How do I read this xml File?

I have this xml file
<?xml version="1.0" encoding="utf-8" ?>
<parameters>
<parameters
registerLink="linkValue"
TextBox.name="nameValue"
/>
</parameters>
I want to print off "LinkValue" and "nameValue" by code:
Console.WriteLine("registerLink: " + registerLink);
Console.WriteLine("TextBox.name: " + TextBox.name);
Thanks
The easiest API is XLinq (System.Xml.Linq)
var doc = XDocument.Load(fileName);
// This should be parameters/parameter, i follow the question with parameters/parameters
var par = doc.Element("parameters").Element("parameters");
registerLink = par.Attribute("registerLink").Value; // string
Your could use an xml reader like this one
http://msdn.microsoft.com/en-us/library/cc189056%28v=vs.95%29.aspx
Once you have a working sample look here to find out how to open an xml reader from a file stream. File must be located in project directory
http://support.microsoft.com/kb/307548
Once you have that done you can add an open file dialog box to find any file on the computer and even validate the .xml extension and more.
Edit: As you can see in the comments below, Hanks solution is better, faster, and easier. My solution would only be useful if you have huge xml files with tons of data. You may still be interested in the file dialog box as well.

How to add a xml in web.config?

I have some complex data which is used for application configuration in xml format. I want to keep this xml string in web.config. Is it possible to add a big xml string in web.config and get it in code everywhere?
If you don't want to write a configuration section handler, you could just put your XML in a custom configuration section that is mapped to IgnoreSectionHandler:
<configuration>
<configSections>
<section
name="myCustomElement"
type="System.Configuration.IgnoreSectionHandler"
allowLocation="false" />
</configSections>
...
<myCustomElement>
... complex XML ...
</myCustomElement>
...
</configuration>
You can then read it using any XML API, e.g. XmlDocument, XDocument, XmlReader classes. E.g.:
XmlDocument doc = new XmlDocument();
doc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
XmlElement node = doc.SelectSingleNode("/configuration/myCustomElement") as XmlElement;
... etc ...
There are several ways of achieving what you want (an XML fragment that is globally and statically accessible to your application code):
The web.config is already an XML file. You can write a custom configuration section (as described here) in order to fetch the data from your custom XML.
You can encode the XML data (all < to <, > to &gt, & to &, " to &quote;)
You can put the XML data in a <![CDATA[]]> section
Don't use web.config for this, but a Settings file as #Yuck commented
That last option is the best one, in terms of ease of development and usage.
The configuration sections in web.config support long strings, but the string has to be a single line of text so they can fit into an attribute:
<add name="" value="... some very long text can go in here..." />
The string also can't contain quotes or line breaks or other XML markup characters. The data is basically XML and it has to be appropriately encoded.
For example, if you have XML like this:
<root>
<value>10</value>
</root>
it would have to be stored in a configuration value like this:
<add key="Value" value="<root>
<value>10</value>
</root>" />
Which kind of defeats the purpose of a configuration element.
You might be better off storing the configuration value in a separate file on the file system and read it from there into a string or XmlDocument etc.

XML tag meaning

I have a part of xml file
<Text><?xml version="1.0" encoding="utf-16"?>
<ObjectFilter xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<FilterConditions>
<FilterCondition>
<PropertyFilters>
<PropertyFilter>
<PropertyName>Message</PropertyName>
<FilterValue xsi:type="xsd:string">PPM exceeds tolerance</FilterValue>
<FilterType>strExpr</FilterType>
<Operator>eq</Operator>
<CaseSensitive>true</CaseSensitive>
<Recursive>false</Recursive>
</PropertyFilter>
</PropertyFilters>
<Enabled>true</Enabled>
<ObjectTypeName>Spo.DataModel.UnixLogMessage</ObjectTypeName>
<ObjectClassGR>
<Guid>00000000-0000-0000-0000-000000000000</Guid>
</ObjectClassGR>
Here what is node Recursive meant,,it actually like this <Recursive>false</Recursive>
but how come it like &lt ;Recursive>false&lt ;/Recursive >
Can any one help me about this
How are you getting this XML file? From a webpage?
It seems that the way you are getting the text file is translating it as an HTML document and thus turning your '<' into &lt and your '>' into &gt
You need to ensure that the page is not interpreted as HTML. You could just copy-paste everything into Notepad first for a simple solution.

Categories