使用WPF注释特定的输入XML文件代码。 [英] Comment specific input XML file code , using WPF .
问题描述
这是我的xml文件:
<?xml version =1.0encoding =utf-8?>
<序列>
<输入>
< Input> readOF< / Input>
<输入> readReference< / Input>
< / Inputs>
< / Steps>
< / Sequence>
我想对此xmlfile行发表评论
<输入> readOF< /输入>
以编程方式使用wpf / c#
我的代码仅在我只有一个输入时才有效:
System.Xml.XmlNode elementToComment = xmlDocument.SelectSingleNode(/ Sequence / Inputs / Input);
但当我有多个输入时无效,
我试图识别我的元素值readOF但我找到了正确的代码格式:
System.Xml.XmlNode elementToComment = xmlDocument.SelectSingleNode( /序列/输入/输入/ readOF);
期望输出:
<! - <输入> readOF < / 输入 > - >
我尝试过:
我的wpf代码:
//找到XML文件的正确路径
String xmlFilePath =path \\ xmfile.xml;
//创建一个XmlDocument
System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
//将XML文件加载到文档
xmlDocument.Load(xmlFilePath);
//使用XPath获取目标节点
System.Xml.XmlNode elementToComment = xmlDocument.SelectSingleNode(/ Sequence / Inputs / Input);
//获取目标节点的XML内容
String commentContents = elementToComment.OuterXml;
//创建一个新的注释节点
//其内容是目标节点的XML内容
System.Xml.XmlComment commentNode = xmlDocument.CreateComment(commentContents);
//获取对目标节点父节点的引用
System.Xml.XmlNode parentNode = elementToComment.ParentNode;
//用注释
parentNode.ReplaceChild(commentNode,elementToComment)替换目标节点;
xmlDocument.Save(xmlFilePath);
MessageBox.Show(ok);
如何在代码中定义元素输入值。
我希望你明白这一点,等待任何帮助。
使用LINQ to XML,你需要做以下几点。 pre lang =c#> 使用系统;
使用 System.Collections.Generic;
使用 System.Xml.Linq;
使用 System.Xml.XPath;
命名空间 DemoApp
{
public class 程序
{
public static void Main( string [] args)
{
string text = @ <?xml version =1.0 encoding =utf-8?>
<序列>
<输入>
<输入> readOF< /输入>
<输入> ; readReference< / Input>
< / Inputs>
< / Sequence>;
XDocument document = XDocument.Parse(text);
IEnumerable< XElement> elements = document.XPathSelectElements( / Sequence / Inputs / Input [。='readOF']) ;
foreach ( var 元素 in elements)
element.ReplaceWith( new XComment(element.ToString()));
Console.WriteLine(document.ToString());
}
}
}
相比之下,这是旧式版......
var document2 = new XmlDocument();
document2.LoadXml(text);
XmlNodeList nodes = document2.SelectNodes(/ Sequence / Inputs / Input [。='readOF']);
foreach(节点中的XmlNode节点)
node.ParentNode.ReplaceChild(document2.CreateComment(node.OuterXml),node);
有关信息在XPATH上,XPathSelectElements
/SelectNodes
中使用的语法,请参阅:
XPath语法 [ ^ ]
我认为你将不得不解析文件,就像它是只是一个文本文件。我会考虑创建一个类,一次一行地将文件加载到List< string>对象,然后使用linq深入查看列表。
你的主要问题是你需要处理至少三个元素定义变体,因为用户是不可预测的:
所有内容都在一行:
< mykey > 值< / mykey >
多行元素:
< mykey >
value
< / mykey >
隐含结束标记的元素:
< mykey text = value / >
这将是一个有趣的编程练习,可以做到这一点,并允许添加/删除元素,移动文件中的元素,注释掉元素,只添加注释等功能。
System.Xml.XmlNode inputs = xmlDocument.SelectSingleNode(/ Sequence / Inputs);
foreach(inputs.ChildNodes中的System.Xml.XmlNode子)
{
if(child.InnerText ==readOF)
{
String commentContents = child .OuterXml;
System.Xml.XmlComment commentNode = xmlDocument.CreateComment(commentContents);
// inputs.RemoveChild(child);
// inputs.PrependChild(commentNode);
child.ParentNode.ReplaceChild(commentNode,child);
休息;
}
}
this is my xml file : <?xml version="1.0" encoding="utf-8"?> <Sequence> <Inputs> <Input>readOF</Input> <Input>readReference</Input> </Inputs> </Steps> </Sequence>
I want to comment this xmlfile line
<Input>readOF</Input>
using wpf/c# programmatically
My code is working only when I have one only Input :
System.Xml.XmlNode elementToComment = xmlDocument.SelectSingleNode("/Sequence/Inputs/Input");
but when I have more than one Input nothing works ,
I tried to identify my element value readOF but I coudnt find the right code format :
System.Xml.XmlNode elementToComment = xmlDocument.SelectSingleNode("/Sequence/Inputs/Input/readOF");
Desired output:
<!--<Input>readOF</Input>-->
What I have tried:
My wpf code :
// Find the proper path to the XML file String xmlFilePath = "path\\xmfile.xml"; // Create an XmlDocument System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument(); // Load the XML file in to the document xmlDocument.Load(xmlFilePath); // Get the target node using XPath System.Xml.XmlNode elementToComment = xmlDocument.SelectSingleNode("/Sequence/Inputs/Input"); // Get the XML content of the target node String commentContents = elementToComment.OuterXml; // Create a new comment node // Its contents are the XML content of target node System.Xml.XmlComment commentNode = xmlDocument.CreateComment(commentContents); // Get a reference to the parent of the target node System.Xml.XmlNode parentNode = elementToComment.ParentNode; // Replace the target node with the comment parentNode.ReplaceChild(commentNode, elementToComment); xmlDocument.Save(xmlFilePath); MessageBox.Show("ok");
How can I define my element Input value in my code .
I wish this is clear for you , waiting for any help .
Using LINQ to XML, you'd want to do the following.using System; using System.Collections.Generic; using System.Xml.Linq; using System.Xml.XPath; namespace DemoApp { public class Program { public static void Main(string[] args) { string text = @"<?xml version=""1.0"" encoding=""utf-8"" ?> <Sequence> <Inputs> <Input>readOF</Input> <Input>readReference</Input> </Inputs> </Sequence>"; XDocument document = XDocument.Parse(text); IEnumerable<XElement> elements = document.XPathSelectElements("/Sequence/Inputs/Input[.='readOF']"); foreach (var element in elements) element.ReplaceWith(new XComment(element.ToString())); Console.WriteLine(document.ToString()); } } }
For comparison, here is the old-style version...
var document2 = new XmlDocument(); document2.LoadXml(text); XmlNodeList nodes = document2.SelectNodes("/Sequence/Inputs/Input[.='readOF']"); foreach (XmlNode node in nodes) node.ParentNode.ReplaceChild(document2.CreateComment(node.OuterXml), node);
For information on XPATH, the syntax used withinXPathSelectElements
/SelectNodes
, see:
XPath Syntax[^]
I think you're going to have to parse the file as if it was just a text file. I would consider creating a class that loads the file one line at a time into a List<string> object, and then use linq to drill down into the list.
Your main problem is that you need to handle at least three element definition variants because users are unpredictable:
Everything on a single line:
<mykey>value</mykey>
Multi-line elements:
<mykey> value </mykey>
Elements with an implicit closing tag:
<mykey text="value" />
It would be an interesting programming exercise that could do that, and allow features such as adding/removing elements, moving elements in the file, commenting out elements, and merely adding comments.
System.Xml.XmlNode inputs = xmlDocument.SelectSingleNode("/Sequence/Inputs"); foreach (System.Xml.XmlNode child in inputs.ChildNodes) { if (child.InnerText == "readOF") { String commentContents = child.OuterXml; System.Xml.XmlComment commentNode = xmlDocument.CreateComment(commentContents); // inputs.RemoveChild(child); // inputs.PrependChild(commentNode); child.ParentNode.ReplaceChild(commentNode, child); break; } }
这篇关于使用WPF注释特定的输入XML文件代码。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!