Save attribute value of xml element with single quotes using linq to xml

二次信任 提交于 2020-01-04 03:55:30

问题


How do I make the XDocument object save an attribute value of a element with single quotes?


回答1:


If it is absolutely necessary to have single quotes you could write your XML document to a string and then use a string replace to change from single to double quotes.




回答2:


I'm not sure that any of the formatting options for LINQ to XML allow you to specify that. Why do you need to? It's a pretty poor kind of XML handler which is going to care about it...




回答3:


As long as you use single- and double-quotes in matched pairs and with correct nesting, standards-compliant XML processors won't care which style you use. Your question suggests that you are intending to process your XML output with tools that are not standards-compliant (or perhaps even not XML-aware). This is a dicey proposition at best, though I recognize that work situations and customer demands may not always give you the options of working with the right tools. I have co-workers who use sed and grep to sift through and modify XML files, and they often can get away with that. But if you have any choice at all, I recommend that you handle XML files with XML-aware tools all along the pipeline up to the point where the data is no longer marked up in XML. Doing otherwise will result in systems that are much more fragile than if you used XML-aware tools for all XML processing.

If you can't do that, then JacobE's suggestion is probably your best bet.



来源:https://stackoverflow.com/questions/222819/save-attribute-value-of-xml-element-with-single-quotes-using-linq-to-xml

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