My code
string workflowIdRef="WorkflowUser_NEW Bình Thuận Copy"
string requestFileXml = HttpContext.Current.Server.MapPath("~/Admin/TS/requestXml/JobCreateReq.xml");
XmlDocument xmld = new XmlDocument();
xmld.Load(requestFileXml);
string requestXml = xmld.OuterXml;
requestXml = requestXml.Replace("WORKFLOW_ID", workflowIdRef);
//Parse string requestXml to xDocument Temp
XDocument xTemp = XDocument.Parse(requestXml);
I debug and see below result
Text Visualizer mode:
And XML visualizer:
The XDocument XTemp has result string like in the picture 2
How to get XTemp to have result string like in the picture 1?
& is special character in XML, it's a marker for beginning of an encoded character like ì. If you want it as plain string as opposed to encoded character you'll need to escape & by using &, for example ì.
demo :
//notice that & are escaped to & in the following string :
string workflowIdRef="WorkflowUser_NEW Bình Thuận Copy"
string workflowIdRef = "WorkflowUser_NEW Bình Thuận Copy";
string xmlContent = #"<root workflowIdRef=""WORKFLOW_ID""/>";
xmlContent.Replace("WORKFLOW_ID", workflowIdRef);.Replace("WORKFLOW_ID", workflowIdRef);
XDocument xTemp = XDocument.Parse(xmlContent);
output :
Related
When I load string xml into XmlDocument object, it throws
'<', hexadecimal value 0x3C, is an invalid attribute character.
string xml= Request.Form["webformfield"] + string.Empty;
// it will read the input from webform in encoded format
e.g:
<Models><Model ModelID="F2434" ModelName="FTest 1 & Income MP" />
try around:
//decoded the whole string
StringWriter sw = new StringWriter();
Server.HtmlDecode(models, sw); // this is an internal method of the framework.
models = sw.ToString();
after decoding the string, the string will be stored below
//string xml = "<Models><Model ModelId=\"124\" ModelNameWithSpecialCHars=\"Test1 <> & \"' characters \"/><Model ModelId=\"124\" ModelNameWithSpecialCHars=\"Test2 <> & \"' characters \"/></Models>";
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xml);
Console.WriteLine(xmlDocument.OuterXml);
I have changed xml string manually at runtime and it worked. Changed value of ModelNameWithSpecialCHars attribute from the string.
added string image, because when I written the encoded specials characters, it was showing it in decoded format. find the below code.
Changed string:
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xml);
Console.WriteLine(xmlDocument.OuterXml);
Console.ReadLine();
Is there any way I can encode only specific part of string. e.g
string xml = "<Models><Model ModelId=\"124\" ModelNameWithSpecialCHars=\"Tes <> & \"' characters \"/></Models>";
in the above string, I need to encode only value of ModelNameWithSpecialCHars attribute. ("Tes <> & "')
Sure - just put the part that needs encoding in a separate variable and use the WebUtility.HtmlEncode method (found in System.Net):
string bad_xml = "Tes <> & \"' characters ";
bad_xml = WebUtility.HtmlEncode(bad_xml);
string xml = "<Models><Model ModelId=\"124\" ModelNameWithSpecialCHars=\"" + bad_xml + "\"/Model></Models>"
although you might find it cleaner to add elements and attributes using the XmlDocument class since you want to have an XmlDocument in the end anyway, and it will encode text for you into valid XML, which has slightly different escaping requirements (although for your test string they are equivalent).
Xml content like following:
<xml>
<item content="abcd
abcd
abcd" />
</xml>
When using XmlDocument to read the content of content attribute,
and
are automatically escaped.
Code:
XmlDocument doc = new XmlDocument();
var content = doc.SelectSingleNode("/xml/item").Attributes["content"].Value;
How can get the raw text without char escaping?
If these characters were written to the lexical XML stream without escaping, then they would be swallowed by the XML parser when the stream is read by the recipient, as a result of the XML line-ending normalisation rules. So you've got it the wrong way around: the reason they are escaped is in order to preserve them; if they weren't escaped, they would be lost.
I got a workaround, it works for me:
private static string GetAttributeValue(XmlNode node, string attributeName)
{
if (node == null || string.IsNullOrWhiteSpace(attributeName))
{
throw new ArgumentException();
}
const string CharLF = "
";
const string CharCR = "
";
string xmlContent = node.OuterXml;
if (!xmlContent.Contains(CharLF) && !xmlContent.Contains(CharCR))
{
// no special char, return its original value directly
return node.Attributes[attributeName].Value;
}
string value = string.Empty;
if (xmlContent.Contains(attributeName))
{
value = xmlContent.Substring(xmlContent.IndexOf(attributeName)).Trim();
value = value.Substring(value.IndexOf("\"") + 1);
value = value.Substring(0, value.IndexOf("\""));
}
return value;
}
Suppose we have this code:
string actualvalue = "<a>";
string abc = "<firstnode><secondnode abc=\""+ actualvalue + "\"></secondnode></firstnode>";
XElement item2 = XElement.Parse(abc);
foreach (var item in item2.Descendants("secondnode"))
{
string aftergettingitfromXlement = item.Attribute("abc").Value;
///so aftergettingitfromXlement = "<a>" which is not correct i want to have actual value which is encoded
}
Any idea how can I get the actual value, I don't want to encode again. Why is the encoding lost?
Try this instead:
string actualvalue = "<a>";
The XDocument implemenation assumes that the value in your xml string is escaped.
To read more about escpae characters in xml: What characters do I need to escape in XML documents?
To do a general replace of ampersands in a string you could write:
string actualvalue = "<a>";
actualvalue = actualvalue.Replace("&", "&");
I have a part of code mentioned like below.
//Reading from a file and assign to the variable named "s"
string s = "<item><name> Foo </name></item>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(s);
But, it stops working if the contents has characters something like "<", ">"..etc.
string s = "<item><name> Foo > Bar </name></item>";
I know, I have to escape those characters before loading but, if I do like
doc.LoadXml(System.Security.SecurityElement.Escape(s));
, the tags (< , >) are also escaped and as a result, the error occurs.
How can I solve this problem?
a tricky solution:
string s = "<item><name> Foo > Bar </name></item>";
s = Regex.Replace(s, #"<[^>]+?>", m => HttpUtility.HtmlEncode(m.Value)).Replace("<","ojlovecd").Replace(">","cdloveoj");
s = HttpUtility.HtmlDecode(s).Replace("ojlovecd", ">").Replace("cdloveoj", "<");
XmlDocument doc = new XmlDocument();
doc.LoadXml(s);
Assuming your content will never contain the characters "]]>", you can use CDATA.
string s = "<item><name><![CDATA[ Foo > Bar ]]></name></item>";
Otherwise, you'll need to html encode your special characters, and decode them before you use/display them (unless it's in a browser).
string s = "<item><name> Foo > Bar </name></item>";
Assign the content of string to the InnerXml property of node.
var node = doc.CreateElement("root");
node.InnerXml = s;
Take a look at - Different ways how to escape an XML string in C#
It looks like the strings that you have generated are strings, and not valid XML. You can either get the strings generated as valid XML OR if you know that the strings are always going to be the name, then don't include the XML <item> and <name> tags in the data.
Then when you create the XMLDocument. do a CreateElement and assign your string before resaving the results.
XmlDocument doc = new XmlDocument();
XmlElement root = doc.CreateElement("item");
doc.AppendChild(root);
XmlElement name = doc.CreateElement("name");
name.InnerText = "the contents from your file";
root.AppendChild(name);
I have an XML document that has the paragraph separator character in some nodes as
When I load XML into an XmlDocument object, I no longer see this character. Instead I see a space. How do I get it to show 
?
XmlDocument doc = new XmlDocument();
doc.Load(xmlFilePath);
XmlNodeList nodes = doc.SelectNodes("/catalog/classes");
foreach(XmlNode node in nodes) {
string category = node["category"];
bool containerSeperator = category.Contains("
") // this should return true but it returns false. This category has a paragraph separator
}
Test #1:
var xmlText = #"<Test>&</Test>";
var xml = XDocument.Parse(xmlText);
var result = xml.Element("Test").Value;
result will not be &, result will be ". So Contains("&") will never be true.
Test #2:
var xmlText = #"<Test>
</Test>";
var xml = XDocument.Parse(xmlText);
var result = Encoding.Unicode.GetBytes(xml.Element("Test").Value);
result will be two bytes: x20 and x29, which is exactly what is read from XML. So the bytes are there you just don't see them as this Unicode character is not readable.