修改现有的xml [英] Modify an existing xml
本文介绍了修改现有的xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
我必须使用C#
解决方案
从sharmarun删除现有xml的声明,
您是否检查了XDocument类可用的所有构造函数? 查看此处
您需要实例没有声明元素/对象的XDocument类。
例如:
XDocument doc = new XDocument();
doc.Add(新XComment(这是评论));
doc.Add(new XElement(Root,content));
Console.WriteLine(doc);
您将获得以下输出:
< br $> b $ b
<! - 这是一条评论 - >
< Root > 内容< / Root >
再见。
XML文件通常有一个标题行,如你所描述的 - 它是可选的,但推荐使用,和XDocument当你使用Save方法时总是添加它。
如果真的让你感到不安,你需要使用XmlWriter - 它可以选择关闭它。 br />
XDocument doc = new XDocument(new XElement( MyElement, new XAttribute( MyKey, MyValue)));
XmlWriterSettings xws = new XmlWriterSettings();
xws.OmitXmlDeclaration = true ;
StringWriter sw = new StringWriter();
使用(XmlWriter xw = XmlWriter.Create(sw,xws))
{
doc.Save(xw);
}
我首先要了解XLinq,因为它是微软最新的XML技术。还有其他方法,但可能想使用最新的: XLINQ简介第3部分,共3部分 [ ^ ]
Hi all
I have to remove declaration from existing xml using C#
解决方案
Hi sharmarun,
Did you check all constructors available for XDocument class? Check here
You need to instance XDocument class with no declaration element/object.
For example:
XDocument doc = new XDocument(); doc.Add(new XComment("This is a comment")); doc.Add(new XElement("Root", "content")); Console.WriteLine(doc);
You will have the following output:
<!--This is a comment--> <Root>content</Root>
Bye.
An XML file normally has a header line such as you describe - it is optional, but recommended, and XDocument always adds it when you use the Save method.
If it really disturbs you, you need to use an XmlWriter instead - it has an option to turn it off.
XDocument doc = new XDocument(new XElement("MyElement", new XAttribute("MyKey", "MyValue"))); XmlWriterSettings xws = new XmlWriterSettings(); xws.OmitXmlDeclaration = true; StringWriter sw = new StringWriter(); using (XmlWriter xw = XmlWriter.Create(sw, xws)) { doc.Save(xw); }
I would start by understanding XLinq since it is the newest XML technology from Microsoft. There are also other ways of doing it, but probably want to use the newest: XLINQ Introduction Part 3 Of 3[^]
这篇关于修改现有的xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文