
近期正在写我的简易标记文件格式的程序,用 xml 文件来当配置文件,结果发现一个事,能把空格写进 xml 文件,但读取的时候发现是空值,并不是空格值。
以下做实验:
string path = @"d:\test\1.xml"XElement xe = new XElement("root",new XElement("a"," "))
Console.WriteLine(xe.ToString())
xe.Save(path)
XElement xt = XElement.Load(path)
string s = xt.Element("a").Value
Console.WriteLine($"12{s}34")
Console.ReadKey()
/*结果:
<root>
<a> </a>
</root>
1234
*/
读取时,空格没有了。查了资料才知道是 xml 文件格式的规定问题。
解决办法:给元素加上属性 xml:space="preserve"。本来想用 XAttribute 来程序写入,出现错误是 不能带 : 符号,没则,只有手工添加。添加后运行:
string s = xt.Element("a").ValueConsole.WriteLine($"12{s}34")
/*结果:
<root xml:space="preserve">
<a> </a>
</root>
12 34
*/
出处:博客园《C# XML 文件中的空格值问题》--乌龙哈里
前两天笔者用IDEA在做springboot一个小的demo的时候,发现自己的mapper.xml文件竟然不起作用!跳出如下错误(捕获错误的样式是之前设置过的):
于是网上一顿搜索,发现可能是以下的原因所致:
一般的原因
Mapper interface和xml文件的定义对应不上,需要检查包名,namespace,函数名称等能否对应上。
按以下步骤一一执行:
1、检查xml文件所在的package名称是否和interface对应的package名称一一对应
2、检查xml文件的namespace是否和xml文件的package名称一一对应
3、检查函数名称能否对应上
4、去掉xml文件中的中文注释
5、随意在xml文件中加一个空格或者空行然后保存
但是竟然都不起作用!崩溃!终于在一篇帖子里看到了想要的答案,在使用IDEA开发时,如果打包时*Mapper.xml没有自动复制到class输出目录的mapper类包下,则需要在pom文件中添加mybatis加载配置文件的配置! 如下所示:
最后就大功告成啦!
由衷感谢度娘上的大佬提供的技术支持!
用JS *** 作XML,对客户端来说只有读取的权限如果需要对XML文件进行修改保存 *** 作,就要用FSO<script type="text/javascript">function go(){var thebook,root,theelem
var xmldoc=new ActiveXObject("Msxml2.DOMDocument.4.0")
xmldoc.async=false
xmldoc.load("C:\\test.xml")
root=xmldoc.documentElement
// alert(xmldoc.xml)
thebook=xmldoc.createElement("book")
// thebook.setAttribute("id" "15")
theelem=xmldoc.createElement("name")
theelem.text="xinshu"
thebook.appendChild(theelem)
theelem=xmldoc.createElement("price")
theelem.text="20"
thebook.appendChild(theelem)
theelem=xmldoc.createElement("momo")
theelem.text="very good!"
thebook.appendChild(theelem)
root.appendChild(thebook)
// alert(xmldoc.xml)thebook=root.selectSingleNode("/books/book[name='xinshu']")
thebook.setAttribute("id","15")
// alert(xmldoc.xml)
thebook=root.selectSingleNode("/books/book[name='哈里波特']")
thebook.childNodes[1].text="20"
thebook.setAttribute("id","25")
// alert(root.xml)
thebook.parentNode.removeChild(thebook)
alert(xmldoc.xml)
var somebook=root.selectNodes("/books/book[price<10]")
// alert(somebook.xml)
somebook.removeAll()
alert(xmldoc.xml)
xmldoc.loadXML(xmldoc.xml)
xmldoc.save("C:\\test.xml") //会提示权限不足..其实我在想 既然DOM给出了这个方法必然有它的用途,但是目前还没发现它在哪能用到 . 试过多个地方使用 都会出现权限问题 可能是JS本身的限制
// var fso, tf //使用JSO必须安装个插件
// fso = new ActiveXObject("Scripting.FileSystemObject")
// 创建新文件
// tf = fso.CreateTextFile("c:\\testfile.xml", true)
// 填写数据,并增加换行符
// tf.WriteLine("Testing 1, 2, 3.")
// 增加3个空行
// tf.WriteBlankLines(3)
// 填写一行,不带换行符
// tf.Write (xmldoc.xml)
// 关闭文件
// tf.Close()
}
</script>其它参考:http://www.68design.net/Web-Guide/HTMLCSS/9335-1.html
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)