Avoid escaping illegal charactes when set an XmlAttribute value - c#

The problem is when a try to set an XmlAttribute value with the '>' char. It escape with the value '>', but '>' es valid because it is into an attribute value. Example:
var element = xmlDoc.CreateElement("Studient");
element.SetAttribute("Year", ">3");
xmlDoc.DocumentElement.AppendChild(element);
xmlDoc.Save(csprojPath);
In this case it produce <Studient Year=">3" xmlns="" />. How can i get <Studient Year=">3" xmlns="" /> ?

As far as I know, There is no way to create the data like ">3" in attribute value. If you open created XML in reader it will display as ">3" only. If you are parsing XML in code , you can easily convert to intended meaning.
If you can use Section value instead of Attribute Value for Student Year, you can use XmlCDataSection by CreateCDataSection (for example: https://msdn.microsoft.com/en-us/library/vstudio/system.xml.xmldocument.createcdatasection(v=vs.100).aspx)

Related

Can I use quote marks in an XML documentation comment?

When writing an XML documentation comment in a C# source file, do I need to replace " with "? This question discusses how to replace characters, but does not establish which characters need replacing, aside from establishing that angle brackets do need replacing.
No, just like normal XML, you only need to escape the quotes when they would otherwise have a special meaning. So if your XML documentation contains an attribute and you want a double quote in the attribute value, then you'd either use an attribute or use a single quote for the value start/end:
/// Foo <element attr="Bar"Baz" />
/// Foo <element attr='Bar"Baz' />
But it's very rare to need attribute values with quotes in within XML documentation, in my experience. They're almost always references to parameters, members, or list types.

serialize attribute without value in C# [duplicate]

I want to have an XML attribute without any value, which simply has one meaning when it exists or does not exist.
Is that valid?
An attribute must be specified with the following syntax:
Name Eq AttValue
where Name is a legal XML name, Eq is = optionally preceded or followed by whitespace, and AttValue is a legal attribute value.
This definition is true for both XML 1.0 and XML 1.1.
If you are trying to specify an attribute as below:
<car owned/>
then no, that is not valid. If you are trying to specify it this way:
<car owned=""/>
then yes, that is valid.
No.
Boolean attributes in XML are of the form foo="foo".
Even in SGML, you must provide the value, (it is the name, = and quotes that you can omit, which is why you have things like <select multiple> in HTML).
You can have an attribute whose only permitted value is the empty string, "". I'm not sure it's good design, though; I would normally suggest a boolean attribute with values true/false, and a default value of false.

XmlElement with special character in innertext

In my c# application i am creating an xml based on the database value. It woks fine until the string is not a special character. Below is my code.
XmlDocument doc = new XmlDocument();
XmlElement element = doc.CreateElement("NewDataSet");
------
string itemname =System.Web.HttpUtility.HtmlEncode(ds.Tables[0].Rows[j]["itemname"].ToString());
fieldElement = doc.CreateElement(itemname);
fieldElement.InnerText = ds.Tables[0].Rows[j]["count"].ToString();
fieldElement1.AppendChild(fieldElement);
where i am getting error in `fieldElement = doc.CreateElement(itemname);
as The ' ' character, hexadecimal value 0x20, cannot be included in a name.
And the string which throws exception is "Adam & Eva frisør".
Can anyone tell me how to overcome this problem.
There are two possible issue here. Firstly, you are html encoding the element name. This will work fine until the element name contains a special character.
The element tags are case-sensitive; the beginning and end tags must
match exactly. Tag names cannot contain any of the characters
!"#$%&'()*+,/;<=>?#[]^`{|}~, nor a space character, and cannot start
with -, ., or a numeric digit.
so, it is an error waiting to happen. Ensure that the name is valid and does not contain any special characters.
Secondly, you are not escaping the content of the element. The text you have quoted contains an unescaped &. This is causing the actual error you are getting. Use the same escape method you are using for the element name for the element text.
fieldElement.InnerText = System.Web.HttpUtility.HtmlEncode(ds.Tables[0].Rows[j]["count"].ToString());
fieldElement1.AppendChild(fieldElement);
where i am getting error in fieldElement.InnerText = ...
I strongly suspect that's not where you're getting the error. Given the part which says "cannot be included in a name" I suspect this is the problem:
fieldElement = doc.CreateElement(itemname);
It's fine to have arbitrary text as text, but not as an element name. it's also unusual to want to create an element name from data. Are you sure you don't want something like:
fieldElement = doc.CreateElement("itemname");
?
& needs to be escaped as & otherwise it is invalid XML.
You should XML encode your database text before putting it into the element. See this question on how to do this.

White space in XmlElement in C# .Net

XmlElement child = doc.CreateElement(element);
Where doc is an object of XmlDocument. When the code executes the above line with element=Tom and Jerry, I get the following error:
The ' ' character, hexadecimal value 0x20, cannot be included in a name.
What should I do to include ' ' in XmlDocument? I can not replace it with anything else.
What are the other characters which XML element does not support for name ?
I suppose you want an element with the value "Tom and Jerry", which is fine.
It is part of the XML syntax that you cannot have a space in the name of an element or attribute.
A possible method:
XmlElement child = doc.CreateElement("cartoon");
child.InnerText = "Tom and Jerry";
which produces
<cartoon>Tom and Jerry</cartoon>
Aside, consider XDocument when you can. Much easier than XmlDocument.
XElement child = new XElement("cartoon", "Tom and Jerry");
It seems your XML element name contains spaces...
This is illegal:
<tom and jerry>hi</tom and jerry>
Must be this:
<tomandjerry>hi</tomandjerry>
U can make the element like this:
<element name='Tom and Jerry' />
But if you need to store some data for this cartoon and have access to it by cartoon name:
<element name='Tom and Jerry'>some data</element>

problem while encoding string to XML

i am gettting problem while encoding string R.F.< 1 into Xml
i am using node.innerxml=string;
it shows exeption name cannot start with 1 or hexadecimal value 0x33.....
pls help me to solve this
The InnerXml property of an XmlNode instance accepts only well-formed XML text. Whenever you attempt to apply the string R.F.< 1, the less-than character is being interpreted as the beginning of an XML element tag. Since an element name cannot begin with a digit, the XML is deemed not to be well-formed, and the set operation fails.
If you wish to put arbitrary text inside an XmlNode, then you should use the InnerText property rather than the InnerXml property. This will ensure that the less-than character is properly escaped (R.F.< 1) in the resulting XML document.
When encoding to XML you need to consider reserved XML characters, and encode them accordingly.
http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references#Predefined_entities_in_XML

Categories

Resources