[英]How can I replace completely one of nodes with new data within fragment xml file?
我是這個論壇的新用戶,我不是經驗豐富的開發人員/程序員。 在問這個問題之前,我搜索並瀏覽了 Stack Overflow 和一些微軟建議的鏈接,但我沒有找到我要找的東西。
我有以下 XML 文件:
<?xml version="1.0" standalone="no" ?>
<WndPos name="FrontEnd.Login" l="703" r="1264" t="323" b="909" />
<LayerManager />
<ViewLayers name="RoofLayout" roof="1" nlwalls="1">
<Layer level="-1" module="1" name="Walls" locked="0" visible="1" />
</ViewLayers>
<DirProfiles>
<ProfileInfo ProfileName="ControlCQFT" JobPath="C:\Jobs" DatabasePath="D:\Database\ControlCQFT" />
</DirProfiles>
<DirHistory>
<ProfileInfo Use="Job" Path="C:\Jobs" />
</DirHistory>
我需要替換整個<DirProfiles>
節點。 傳入節點可以為空,類似於<DirProfiles />
。 我要插入的新節點的格式為:
<DirProfiles>
<ProfileInfo ProfileName="Control1" JobPath="D:\Client1\JobsA" DatabasePath="D:\Database\Control1" />
. . . . .
</DirProfiles>
我試圖將這個問題作為一個簡單的字符串替換來處理,但我沒有得到我需要的結果。
編輯:抱歉,我沒有意識到我的 xml 文件是片段 xml 文檔,而不是符合規范的完整 XML。
鑒於您的輸入文件是一個不包含根節點的格式錯誤的 XML 文件,您可以通過一些字符串操作來解決這個問題。
這是你說的文件:
<?xml version="1.0" standalone="no" ?>
<WndPos name="FrontEnd.Login" l="703" r="1264" t="323" b="909" />
<LayerManager />
<ViewLayers name="RoofLayout" roof="1" nlwalls="1">
<Layer level="-1" module="1" name="Walls" locked="0" visible="1" />
</ViewLayers>
<DirProfiles>
<ProfileInfo ProfileName="ControlCQFT" JobPath="C:\Jobs" DatabasePath="D:\Database\ControlCQFT" />
</DirProfiles>
<DirHistory>
<ProfileInfo Use="Job" Path="C:\Jobs" />
</DirHistory>
以下是如何使用它:
var sourceFileName = @"C:\{path}\xml_fragments.txt";
var text = $"<root>{String.Join(Environment.NewLine, File.ReadLines(sourceFileName).Skip(1))}</root>";
var doc = XDocument.Parse(text);
doc.Root.Element("DirProfiles").Elements().Remove();
doc.Root.Element("DirProfiles").Add(
new XElement(
"ProfileInfo",
new XAttribute("ProfileName", "Control1"),
new XAttribute("JobPath", @"D:\Client1\JobsA"),
new XAttribute("DatabasePath", @"D:\Database\Control1")));
這給了我:
<root>
<WndPos name="FrontEnd.Login" l="703" r="1264" t="323" b="909" />
<LayerManager />
<ViewLayers name="RoofLayout" roof="1" nlwalls="1">
<Layer level="-1" module="1" name="Walls" locked="0" visible="1" />
</ViewLayers>
<DirProfiles>
<ProfileInfo ProfileName="Control1" JobPath="D:\Client1\JobsA" DatabasePath="D:\Database\Control1" />
</DirProfiles>
<DirHistory>
<ProfileInfo Use="Job" Path="C:\Jobs" />
</DirHistory>
</root>
請參閱以下內容:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
string xml = File.ReadAllText(FILENAME);
XElement doc = new XElement("Root");
doc.Add(XElement.Parse(xml));
XElement dirProfiles = doc.Descendants("DirProfiles").FirstOrDefault();
XElement profileInfo = dirProfiles.Element("ProfileInfo");
profileInfo.SetAttributeValue("ProfileName", "Control1");
profileInfo.SetAttributeValue("JobPath", @"D:\Client1\JobsA");
profileInfo.SetAttributeValue("DatabasePath", @"D:\Database\Control1");
}
}
}
您可以將 XML 數據解析為 C# 類模型,然后可以根據需要進行數據更改。 更改后,您可以再次將模型對象解析為 XML 並在同一文件中重寫 XML 字符串。
請參閱此鏈接以通過可用的 XML 生成類結構: https : //www.c-sharpcorner.com/blogs/convert-xml-json-file-to-c-sharp-class
並使用此代碼將 Xml 轉換為 C# 類對象
public static T ConvertXmlToObject<T>(String xml)
{
T result = default(T);
try
{
using (TextReader reader = new StringReader(xml))
{
try
{
result =
(T)new XmlSerializer(typeof(T)).Deserialize(reader);
}
catch (InvalidOperationException)
{
// Throw message for invalid XML
}
}
}
catch (Exception ex)
{
}
return result;
}
調用函數,如:
var entity = ConvertXmlToObject<ModelClass>(XMLString);
使用此代碼再次將對象轉換為 XML
public static string ConvertObjectToXML<T>(T ModelClass)
{
XmlSerializer xsObject = new XmlSerializer(typeof(T));
var inputObject = ModelClass;
var xmlString = "";
using (var sw = new StringWriter())
{
using (XmlWriter writer = XmlWriter.Create(sw))
{
xsObject.Serialize(writer, inputObject);
xmlString = sw.ToString();
}
}
return xmlString;
}
像這樣調用這個函數:
string xmlString = ConvertObjectToXML<ModelClass>(ModelClassObject);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.