C#-XML节点替换 [英] C# - XML Node Replace

查看:88
本文介绍了C#-XML节点替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我的代码如下:
_____________________________________

Hi All,
I have this piece of code below:
_____________________________________

XmlDocument doc = new XmlDocument();
           string sFileName = @"C:\temp\testFile.xml";

           doc.Load(sFileName);

           XmlNodeList textNodes = doc.SelectNodes("//fbkMetaValueT");

           foreach (DataRow currentRow in ds.Tables[0].Rows)
           {
               string toTranslate = currentRow[3].ToString().Trim();

               for (int columnCounter = 4; columnCounter < ds.Tables[0].Columns.Count; columnCounter++)
               {
                   string countryCode = ds.Tables[0].Columns[columnCounter].ColumnName;
                   string translation = currentRow[columnCounter].ToString().Trim();

                   foreach (XmlNode currentNode in textNodes)
                   {
                       string longText = currentNode.Attributes[0].Value.Trim();
                       string locale = currentNode.Attributes[2].Value.Trim();

                       // strip out apostrophes
                       longText.Replace("'", "");
                       translation.Replace("'", "");
                       toTranslate.Replace("'", "");

                       if ((longText == toTranslate) && (locale == countryCode))
                       {
                           currentNode.Attributes[0].Value = translation;
                       }
                   }
               }
           }


           XmlWriterSettings settings = new XmlWriterSettings();
           settings.Encoding = Encoding.UTF8;
           settings.Indent = true;


           using (XmlWriter writer = XmlWriter.Create("c:\\temp\\test.xml",settings))
           {
               doc.WriteContentTo(writer);
               writer.Flush();
               writer.Close();
           }


________________________________________________
本质上,我正在做的是扫描数据集并更新现有XML文件中的Node Value(我已经读得很好),但是我只想更新某些节点值与我的值匹配的节点数据集.这可以正常工作-使用除包含撇号字符的字符串以外的所有字符串.进行匹配的行是:
if ((longText == toTranslate) && (locale == countryCode))
将longText字符串与toTranslate字符串进行比较.这是两个包含撇号字符的字符串.我知道我要更新的值也始终位于属性"列表的[0]上
我已经进行了一些调试,这些值看起来完全相同.
谁可以帮我这个事?非常紧急,因此任何紧急帮助将不胜感激
非常感谢,
Dan


________________________________________________
Esentially what I''m doing is scanning through a dataset and updating a Node Value within an existing XML file (which I''ve read in fine) - however I only want to update certain nodes where that node value matches a value in my dataset. This works fine - with all strings apart from those that contain an apostrophe character. The line that does the matching is:
if ((longText == toTranslate) && (locale == countryCode))
The longText string is compared to the toTranslate string. These are the two strings that contain the apostrophe characters. I know the value I want to update is also always located at [0] on the Attribute list
I''ve done some debugging and these values look exactly the same.
Can anyone help me on this? It''s pretty urgent so any urgent help would be greatly appreciated
Many Thanks,
Dan

推荐答案

for循环的第一行将countryCode设置为数据库中列的 ColumnName .那是你想要的吗?在没有看到您的数据的情况下,这听起来有点奇怪. :)
The first line in your for loop sets countryCode to the ColumnName of the column from the database. Is that what you intended? it sounds a little odd to me, without having seen your data. :)


这实际上是正确的-每个国家/地区都有自己的数据列
That is actually correct - each country has its own column of data


这篇关于C#-XML节点替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆