C# Xml不';无法保存到文件
我正在用Xamarin编写Android应用程序 我有正在写入文件的xml(已实现) 在不同的活动中,我尝试打开此文件,替换一些字符串并保存 像这样打开文件C# Xml不';无法保存到文件,c#,android,xml,xamarin,xamarin.android,C#,Android,Xml,Xamarin,Xamarin.android,我正在用Xamarin编写Android应用程序 我有正在写入文件的xml(已实现) 在不同的活动中,我尝试打开此文件,替换一些字符串并保存 像这样打开文件 var doc2 = new XmlDocument(); var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); var filePath = System.IO.Pat
var doc2 = new XmlDocument();
var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var filePath = System.IO.Path.Combine(documentsPath, "myFile.xml");
doc2.Load (filePath);
替换某些字符串,如下所示:
string str;
str = doc2.OuterXml;
str = str.Replace ("{ProductCode}", Code1);
Console.WriteLine ("look");
Console.WriteLine (str);
doc2.Save (filePath);
Console.WriteLine (doc2.OuterXml);
当我显示str时,我看到“ProductCode”发生了变化
但是当我显示“doc2.OuterXML”时,我看到它没有保存
这是“str”:
为什么不保存?您尚未修改文档。您已经要求文档提供其自身的字符串表示形式,然后为同一变量分配了一个新字符串——但这根本不会改变文档中的XML 我强烈建议您使用LINQ to XML(这是一种更好的XML API),此时您可以:
XDocument doc = XDocument.Load(filePath);
var query = doc.Descendants("Product")
.Where(p => (string) p.Attribute("Code") == "{ProductCode}");
foreach (var element in query)
{
element.SetAttributeValue("Code", Code1);
}
doc.Save(filePath);
您尚未修改文档。您已经要求文档提供其自身的字符串表示形式,然后为同一变量分配了一个新字符串——但这根本不会改变文档中的XML 我强烈建议您使用LINQ to XML(这是一种更好的XML API),此时您可以:
XDocument doc = XDocument.Load(filePath);
var query = doc.Descendants("Product")
.Where(p => (string) p.Attribute("Code") == "{ProductCode}");
foreach (var element in query)
{
element.SetAttributeValue("Code", Code1);
}
doc.Save(filePath);
你漏了一行
doc2.OuterXml = str;
您的代码是OuterXml
中的str
string str;
str = doc2.OuterXml;
str = str.Replace ("{ProductCode}", Code1);
然后替换了str值,但没有替换doc2值,因此保持不变。因此,现在您需要添加
string str;
str = doc2.OuterXml;
str = str.Replace ("{ProductCode}", Code1);
doc2.OuterXml = str;
现在您将看到doc2.OuterXml也将给出相同的结果。您有一行遗漏
doc2.OuterXml = str;
您的代码是OuterXml
中的str
string str;
str = doc2.OuterXml;
str = str.Replace ("{ProductCode}", Code1);
然后替换了str值,但没有替换doc2值,因此保持不变。因此,现在您需要添加
string str;
str = doc2.OuterXml;
str = str.Replace ("{ProductCode}", Code1);
doc2.OuterXml = str;
现在您将看到doc2.OuterXml也将提供相同的结果。请在格式化代码和XML方面投入更多精力。代码现在到处都是,每个XML文档都在一行上……您需要将
str
保存回doc2
。即doc2.OuterXml=str
。由于c#字符串是不可变的,因此Replace
调用返回一个新字符串,而不是修改原始字符串。虽然实际上我根本不会这样做,但我会使用XmlDocument
API本身来进行修改。@dbc当我写这篇文章时,我有一个错误。错误CS0200:无法将属性或索引器“System.Xml.XmlNode.OuterXml”分配给--它是只读的,可能需要使用。或者根本不这样做,使用下面的XDocument
。(或者使用然后执行字符串替换。如果要立即将XML转换回字符串,为什么还要解析XML呢?)请在格式化代码和XML方面投入更多精力。代码现在到处都是,每个XML文档都在一行上……您需要将str
保存回doc2
。即doc2.OuterXml=str
。由于c#字符串是不可变的,因此Replace
调用返回一个新字符串,而不是修改原始字符串。虽然实际上我根本不会这样做,但我会使用XmlDocument
API本身来进行修改。@dbc当我写这篇文章时,我有一个错误。错误CS0200:无法将属性或索引器“System.Xml.XmlNode.OuterXml”分配给--它是只读的,可能需要使用。或者根本不这样做,使用下面的XDocument
。(或者使用然后执行字符串替换。如果要立即将XML转换回字符串,为什么还要解析它呢?)Xamarin允许使用XDocument?@Eugene:是的,我肯定它会。@Eugene:是的。您只需要正确的程序集引用和使用指令。什么程序集引用?当我写XDocument时,它变为XmlDocument@Eugene:首先,您需要System.Xml.Linq的using指令。。。您可能会发现程序集引用已经存在。Xamarin允许使用XDocument吗?@Eugene:是的,我确信它确实存在。@Eugene:是的。您只需要正确的程序集引用和使用指令。什么程序集引用?当我写XDocument时,它变为XmlDocument@Eugene:首先,您需要System.Xml.Linq的using指令。。。您可能会发现程序集引用已经存在。我在编译时出错。错误CS0200:无法将属性或索引器“System.Xml.XmlNode.OuterXml”分配给--它是只读的。编译时我出错。错误CS0200:无法将属性或索引器“System.Xml.XmlNode.OuterXml”分配给--它是只读的