C# 如果utf-16 xml文件在主节点结束后有字符串行,如何编辑该文件
我有一个带有C# 如果utf-16 xml文件在主节点结束后有字符串行,如何编辑该文件,c#,xml,declaration,utf-16,innertext,C#,Xml,Declaration,Utf 16,Innertext,我有一个带有utf-16编码类型的特殊XML文件。该文件用于存储数据,我需要使用C#windows窗体应用程序对其进行编辑 <?xml version="1.0" encoding="utf-16"?> <cProgram xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ID="b0eb0c7e-f4de-4bc7-9e62
utf-16
编码类型的特殊XML文件。该文件用于存储数据,我需要使用C#windows窗体应用程序对其进行编辑
<?xml version="1.0" encoding="utf-16"?>
<cProgram xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ID="b0eb0c7e-f4de-4bc7-9e62-7a086a8c2fn8" Version="16.01" xmlns="cProgram">
<Serie>N </Serie>
<No>123456</No>
<type>101</type>
<Dataset4>larg data here 2 million char</Dataset4>
</cProgram>123456FF896631N 4873821012013-06-14
当我尝试将其作为xml文件加载时
我试图临时替换最后一行,并在我更改内部文本后将其取回,但它工作正常,但我丢失了声明行,当我在文件末尾有文本时,我没有找到重写它的方法_因此,我需要更改(Serie)和(No)节点的
InnerText
但我不想丢失文件末尾的声明行或字符串文本请尝试以下代码:
string line = "";
string[] stringsperate = new string[] { "</cProgram>" };
using (StreamReader sr = new StreamReader("C://blah.xml"))
{
line = sr.ReadToEnd();
Console.WriteLine(line);
}
string text = line.Split(stringsperate, StringSplitOptions.None)[0];
text += "</cProgram>";
XmlDocument xd = new XmlDocument();
xd.LoadXml(text);
Console.Read();
字符串行=”;
字符串[]stringsperate=新字符串[]{”“};
使用(StreamReader sr=newstreamreader(“C://blah.xml”))
{
line=sr.ReadToEnd();
控制台写入线(行);
}
string text=line.Split(stringsperate,StringSplitOptions.None)[0];
文本+=”;
XmlDocument xd=新的XmlDocument();
LoadXml(文本);
Console.Read();
希望这有帮助XDocument.Save()
应该保留XML声明行(如果声明最初存在)。我还检查了您的XML和按预期保存的声明行:
var xml = @"<?xml version=""1.0"" encoding=""utf-16""?>
<cProgram xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" ID=""b0eb0c7e-f4de-4bc7-9e62-7a086a8c2fn8"" Version=""16.01"" xmlns=""cProgram"">
<Serie>N </Serie>
<No>123456</No>
<type>101</type>
<Dataset4>larg data here 2 million char</Dataset4>
</cProgram>";
var doc = XDocument.Parse(xml);
doc.Save("test.xml");
var xml=@”
N
123456
101
这里的大数据是200万字符
";
var doc=XDocument.Parse(xml);
doc.Save(“test.xml”);
因此,您可以实现临时替换最后一行的想法,并在更改内部文本后将其取回
仅供参考,
XDocument
的.ToString()
方法不编写XML声明行,但.Save()
方法编写XML声明行。与此相关的问题:请允许我回答我的问题
当我使用doc.Load(filepath)时;它总是给出干扰最后一行的错误原因
C#使用UTF-8作为默认值来处理xml文件
因此,我找到了一个非常简单的方法来实现这一点&根据需要将innertext替换为string
string text = File.ReadAllText(filepath);
text = text.Replace("<Serie>N", "<Serie>"+textBox1.Text);
text = text.Replace("<Nom>487382","<Nom>"+textBox2.Text);
//saving file with UTF-16
File.WriteAllText("new.xml", text , Encoding.Unicode);
string text=File.ReadAllText(filepath);
text=text.Replace(“N”,“”+textBox1.text);
text=text.Replace(“487382”,“textBox2.text”);
//使用UTF-16保存文件
File.writealText(“new.xml”,text,Encoding.Unicode);
与此[博客]相关的问题:“与答案相关比与问题相关要多得多”A.Khatri:这不管用。。可能是因为它是一个大文件(dataset4中有两百万个字符),而且它没有说明如何更改内部文本!使用您提供的xml,它可以正常工作。你必须尝试编辑内部文本的工作,因为谷歌是充满帮助的。也许你想了解我。。我知道如何更改内部文本,但使用此文件我无法更改,我的问题是如何更改它。谷歌上并没有这方面的内容,这是最后一行的特殊原因!它是在文件末尾还是在
之后?两者都有。这是XML文件中的最后一行,在我提供的“”之后。我发现问题出在哪里。。它是在编码C#使用UTF-8作为默认值。文本是UTF-16,我的xml文件也是UTF-16。谢谢你的帮助,期待我的新权力
string text = File.ReadAllText(filepath);
text = text.Replace("<Serie>N", "<Serie>"+textBox1.Text);
text = text.Replace("<Nom>487382","<Nom>"+textBox2.Text);
//saving file with UTF-16
File.WriteAllText("new.xml", text , Encoding.Unicode);