C# 将 XML 字符串块附加到现有的 XmlDocument
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/179173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Append XML string block to existing XmlDocument
提问by Howler
I have an XmlDocument that already exists and is read from a file.
我有一个已经存在并从文件中读取的 XmlDocument。
I would like to add a chunk of Xml to a node in the document. Is there a good way to create and add all the nodes without cluttering my code with many .CreateNote and .AppendChild calls?
我想向文档中的节点添加一大块 Xml。有没有一种好方法来创建和添加所有节点,而不会用许多 .CreateNote 和 .AppendChild 调用使我的代码混乱?
I would like some way of making a string or stringBuilder of a valid Xml section and just appending that to an XmlNode.
我想要某种方法来制作有效 Xml 部分的字符串或 stringBuilder,并将其附加到 XmlNode。
ex: Original XmlDoc:
例如:原始 XmlDoc:
<MyXml>
<Employee>
</Employee>
</MyXml>
and, I would like to add a Demographic (with several children) tag to Employee:
并且,我想为员工添加一个人口统计(有几个孩子)标签:
<MyXml>
<Employee>
<Demographic>
<Age/>
<DOB/>
</Demographic>
</Employee>
</MyXml>
采纳答案by user7116
I suggest using XmlDocument.CreateDocumentFragmentif you have the data in free form strings. You'll still have to use AppendChild to add the fragment to a node, but you have the freedom of building the XML in your StringBuilder.
如果您有自由格式字符串的数据,我建议使用XmlDocument.CreateDocumentFragment。您仍然需要使用 AppendChild 将片段添加到节点,但您可以在 StringBuilder 中自由构建 XML。
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(@"<MyXml><Employee></Employee></MyXml>");
XmlDocumentFragment xfrag = xdoc.CreateDocumentFragment();
xfrag.InnerXml = @"<Demographic><Age/><DOB/></Demographic>";
xdoc.DocumentElement.FirstChild.AppendChild(xfrag);
回答by Panos
Try this:
尝试这个:
employeeNode.InnerXml = "<Demographic><Age/><DOB/></Demographic>";
Alternatively (if you have another XML document that you want to use):
或者(如果您有另一个要使用的 XML 文档):
employeeNode.AppendChild(employeeNode.OwnerDocument.ImportNode(otherXmlDocument.DocumentElement, true));
回答by Jeff Yates
Consider using an XmlWriter for building your fragments on a StringBuilder as this will provide validation and character substitution for you.
考虑使用 XmlWriter 在 StringBuilder 上构建片段,因为这将为您提供验证和字符替换。
回答by Echostorm
As an alternative, this is how you could do it in a more LINQy 3.5 manner:
作为替代方案,您可以通过以下方式以更 LINQy 3.5 的方式执行此操作:
XDocument doc = XDocument.Load(@"c:\temp\test.xml");
XElement demoNode = new XElement("Demographic");
demoNode.Add(new XElement("Age"));
demoNode.Add(new XElement("DOB"));
doc.Descendants("Employee").Single().Add(demoNode);
doc.Save(@"c:\temp\test2.xml");
回答by Echostorm
All that I do is creating a new dataset object and open the xml file using ReadXML myDataset.ReadXML(path and file name)
.
我所做的就是创建一个新的数据集对象并使用 ReadXML 打开 xml 文件myDataset.ReadXML(path and file name)
。
Then add or remove the rows that I need and save the document again using myDataset.WriteXML(path and file name)
.
然后添加或删除我需要的行并使用myDataset.WriteXML(path and file name)
.
Bye.
再见。
回答by Peter Gruppelaar
None of this was working for me so i played around abit and here is my solution.
这些都不适合我,所以我玩了一下,这是我的解决方案。
First load up a text field(you can put it to visible = false in public version) load the data in to the text field like so.
首先加载一个文本字段(您可以在公共版本中将其设置为visible = false)将数据加载到文本字段中,如下所示。
string Path = Directory.GetCurrentDirectory() + "/2016";
string pathFile = Path + "/klanten.xml";
StreamReader sr = new StreamReader(pathFile);
txt.Text = sr.ReadToEnd();
sr.Close();
on the save button load up the text field en save it. Dont forget u will have to refresh the text field after that, if u want to add multiple addresses/names, i have not included that part.
在保存按钮上加载文本字段并保存它。不要忘记之后你必须刷新文本字段,如果你想添加多个地址/名称,我没有包括那部分。
string name = Globals.s_Name;
string klanten = txt.Text;
string s = klanten;
XmlDocument xdoc = new XmlDocument();
string klant = "<voornaam>" + naamBox1.Text + "</voornaam>";
xdoc.LoadXml(s);
XmlDocumentFragment xfrag = xdoc.CreateDocumentFragment();
xfrag.InnerXml = klant;
xdoc.DocumentElement.FirstChild.AppendChild(xfrag);
xdoc.Save(name + "/klanten.xml");