i have some trouble to edit an XML.
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.
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:
<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 XML, I'm open for better options
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
}
}
CodePudding user response:
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
.
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.
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.
You can then write the changes back to the XML file using XmlWriter
.
Keep in mind that these classes work with in-memory versions of the XML file.
EDIT: I just realized that XmlDocument
has helper functions to perform all of these actions directly. 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.
CodePudding user response:
One popular library is the 'System.Xml' namespace, which is part of the .NET framework. 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.
Another popular library is LINQ to XML, which provides an API for querying and manipulating XML using the LINQ (Language Integrated Query) syntax. You can use the 'XElement' class to represent an XML element, and the 'XDocument' class to represent an XML document.
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#.
Here is an example of how you can use the XmlDocument class to edit an 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");