Formatting string in XML format and remove invalid attribute characters

感情迁移 提交于 2019-12-13 08:28:11

问题


I've a string say "<Node a="<b>">". I need to escape only the data and parse this string as a node in XMLWriter. Hence how to escape only the attribute value "<" and note the XML structure's "<".


回答1:


using (var writer = XmlWriter.Create(Console.Out))
{
    writer.WriteStartElement("Node");
    writer.WriteAttributeString("a", "<b>");
}

Output <Node a="&lt;b&gt;" />


Firstly you should parse the string. Since this is not valid xml, you can't use an xml parser. You can try HtmlAgilityPack. Then you can write the values with xml writer.

string s = "<Node a=\"<b>\">";

var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(s);

var node = doc.DocumentNode.FirstChild;
var attr = node.Attributes[0];

using (var writer = XmlWriter.Create(Console.Out))
{
    writer.WriteStartElement(node.Name);
    writer.WriteAttributeString(attr.Name, attr.Value);
}


来源:https://stackoverflow.com/questions/36979441/formatting-string-in-xml-format-and-remove-invalid-attribute-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!