简体   繁体   English

Xml 文字编辑C#

[英]Xml Text Edit in C#

i have some trouble to edit an file.我在编辑文件时遇到了一些麻烦。

i tried to scan for " Module Name="+0.1.2HUB" Type="0AC808.9" " in an long XML if the text is found i would like to insert an text the next line.我尝试在长 XML 中扫描“ Module Name="+0.1.2HUB" Type="0AC808.9" ”,如果找到文本,我想在下一行插入文本。

if not write this:如果不写这个:


<Module Name="+0.1.2HUB" Type="0AC808.9" Frozen="true" LastFrozenVersion="1.0.0.0" Version="1.0.0.0">
 <Connection Connector="ETH1" TargetModule="+0.1.2HUB" TargetConnector="PLK1" />
    <Cable Type="PowerlinkCable" Length="10" Version="1.0.0.3" />
 </Connection>
 <Connection Connector="ETH8" TargetModule="80274418TLS03" TargetConnector="IF3">
    <Cable Type="PowerlinkCable" Length="10" Version="1.0.0.3" />
 </Connection>
</Module>

The string to be inset loos like this:要插入的字符串如下所示:

 <Connection Connector="ETH2" TargetModule="+0.1.2HUB" TargetConnector="PLK1" />
    <Cable Type="PowerlinkCable" Length="10" Version="1.0.0.3" />
 </Connection>

The optimal result would be this if the scanresult is True:如果扫描结果为 True,最佳结果将是这样的:

<Module Name="+0.1.2HUB" Type="0AC808.9" Frozen="true" LastFrozenVersion="1.0.0.0" Version="1.0.0.0">
 <Connection Connector="ETH1" TargetModule="+0.1.2HUB" TargetConnector="PLK1" />
    <Cable Type="PowerlinkCable" Length="10" Version="1.0.0.3" />
 </Connection>
 <Connection Connector="ETH2" TargetModule="+0.1.2HUB" TargetConnector="PLK1" />
    <Cable Type="PowerlinkCable" Length="10" Version="1.0.0.3" />
 </Connection>
 <Connection Connector="ETH8" TargetModule="80274418TLS03" TargetConnector="IF3">
    <Cable Type="PowerlinkCable" Length="10" Version="1.0.0.3" />
 </Connection>

At the moment im try to use the streamreader too read the file, I'm open for better options目前我尝试使用流阅读器来读取文件,我愿意接受更好的选择

EDIT: its not an XML file, ony the inner part is an XML.编辑:它不是 XML 文件,只有内部是 XML。

                using (StreamReader sr = new StreamReader(pathFile))
                {
                    string line;
                    // Read and display lines from the file until the end of
                    // the file is reached.
                    while ((line = sr.ReadLine()) != null)
                    {
                        string compare = "<Module Name=\"" + moduleName + "\" Type=\"0AC808.9\"";
         
                        // look for an string 
                        if (line.Contains(compare))
                        {
                            return True;
                        }
                    }
                    return False; // modul not found
                }
            }

There are a number of built-in .NET classes to handle reading, writing, and validating XML files without having to parse them manually: XmlReader , XmlWriter , and XPathNavigator .有许多内置的 .NET 类可以处理读取、写入和验证 XML 文件而无需手动解析它们: XmlReaderXmlWriterXPathNavigator

In any case, I suggest you use XmlDocument from your file, and use that to perform any validations, modifications, etc. that you may need to do.无论如何,我建议您使用文件中的XmlDocument ,并使用它来执行您可能需要执行的任何验证、修改等。

You will be reading your XML file using XmlReader and then loading the data from there into your XmlDocument which will allow for easy manipulation of the data.您将使用XmlReader读取您的 XML 文件,然后将数据从那里加载到您的XmlDocument中,这样可以轻松处理数据。

You can then write the changes back to the XML file using XmlWriter .然后,您可以使用XmlWriter将更改写回 XML 文件。

Keep in mind that these classes work with in-memory versions of the XML file.请记住,这些类适用于 XML 文件的内存版本。

EDIT: I just realized that XmlDocument has helper functions to perform all of these actions directly.编辑:我刚刚意识到XmlDocument有帮助函数来直接执行所有这些操作。 For example:例如:

var myXmlFile = new XmlDocument();
myXmlFile.Load("myLocalFile.xml");

will load the file.将加载文件。 The XmlDocument page linked above gives very detailed example on all of the possible actions.上面链接的 XmlDocument 页面提供了所有可能操作的非常详细的示例。

One popular library is the 'System.Xml' namespace, which is part of the .NET framework.一个流行的库是“System.Xml”命名空间,它是 .NET 框架的一部分。 This namespace provides a set of classes for reading and writing 'XML documents', including the XmlDocument class, which represents an XML document and allows you to edit its contents.这个命名空间提供了一组用于读取和写入“XML 文档”的类,包括 XmlDocument class,它表示一个 XML 文档并允许您编辑其内容。

Another popular library is LINQ to XML, which provides an API for querying and manipulating XML using the LINQ (Language Integrated Query) syntax.另一个流行的库是 LINQ 到 XML,它提供了一个 API,用于使用 LINQ(语言集成查询)语法查询和操作 XML。 You can use the 'XElement' class to represent an XML element, and the 'XDocument' class to represent an XML document.您可以使用“XElement”class 来表示 XML 元素,使用“XDocument”class 来表示 XML 文档。

You can also use a third-party library such as XmlTextEdit that provide more feature and more flexibility when working with XML text in C#.您还可以使用第三方库,例如 XmlTextEdit,它在处理 C# 中的 XML 文本时提供更多功能和更多灵活性。

Here is an example of how you can use the XmlDocument class to edit an XML这是一个示例,说明如何使用 XmlDocument class 编辑 XML

XmlDocument doc = new XmlDocument();
doc.Load("example.xml");

XmlNode root = doc.DocumentElement;

XmlNodeList nodes = root.SelectNodes("/books/book");

foreach (XmlNode node in nodes)
{
   XmlElement element = (XmlElement)node;
   element.SetAttribute("author", "John Smith");
}

doc.Save("example.xml");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM