需要C#XML更改帮助 [英] C# XML Changes Help Required

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

问题描述

我有一个包含XML的字符串(字符串stringXML),我将其加载到XML文档中,并在该XML文档中进行动态更改,

I have a string containing XML(string stringXML), I load it in an XML document and make changes in that XML document dynamically,

XmlDocument doc = new XmlDocument();
            doc.LoadXml(stringXML);
            var fnode = doc.GetElementsByTagName("feature")[0];
            var fval = fnode.Attributes["name"].Value;
            if (fval == "IMAGING_SERVICES")
            {
                var servcnode = doc.GetElementsByTagName("service")[0];
                var namenode = servcnode.Attributes["name"].Value;
                if (namenode == "PRINTING")
                {
                    doc.InnerXml.Replace(namenode, "IMAGING_OPTIONS");
                }
            }




然后我需要将stringXML 发送给服务器并进行更改,出现的问题是它没有更改doc的值.
更改文档后,第二个方法如何在stringXML中进行更改?




Then i need to send stringXML to the server with changes in it, the problem arising is that it is not changing value of doc.
Second after changing in doc how can i also make changes in stringXML?

推荐答案

更改XML后,doc.OuterXml将更新字符串. />
After changing your XML, doc.OuterXml will have the updated the String.

XmlDocument doc = new XmlDocument();
            doc.LoadXml(stringXML);
            var fnode = doc.GetElementsByTagName("feature")[0];
            var fval = fnode.Attributes["name"].Value;
            if (fval == "IMAGING_SERVICES")
            {
                var servcnode = doc.GetElementsByTagName("service")[0];
                var namenode = servcnode.Attributes["name"].Value;
                if (namenode == "PRINTING")
                {
                    doc.InnerXml.Replace(namenode, "IMAGING_OPTIONS");
                }
            }

String newXmlString = doc.OuterXml;



newXmlString将具有更新的XML.

您是否也要保存该文档?
然后,您可以使用新的XmlString重新加载文档:
doc.LoadXml(newXmlString);



The newXmlString will have the updated XML.

Do you want to save the doc too?
Then you can reload the doc with the new XmlString :
doc.LoadXml(newXmlString);


我不明白这个问题:您正在使用的DOM中的每个节点和属性都是可修改的.只需查看相关类的帮助即可.

—SA
I don''t event understand the problem: every node and attribute in DOM you''re using is modifiable. Just look at help on related classes.

—SA


一切都按预期进行:
如果从字符串构造了文档之后更改了stringXML,则必须重复doc.LoadXml(stringXML).
如果您对doc进行更改并希望将其显示在stringXML中,则必须再次将doc呈现为字符串.

最好的问候,

-MRB
Everything is working as expected:
If you change stringXML after the document has been constructed from the string you would have to repeat doc.LoadXml(stringXML).
If you make changes to doc and want them manifested in stringXML you''d have to render doc to a string again.

Best Regards,

-MRB


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

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