Tag: xml

ConfigurationManager可以在Save()上保留XML注释吗?

我写了一个小实用程序,允许我为另一个应用程序的App.config文件更改一个简单的AppSetting,然后保存更改: //save a backup copy first. var cfg = ConfigurationManager.OpenExeConfiguration(pathToExeFile); cfg.SaveAs(cfg.FilePath + “.” + DateTime.Now.ToFileTime() + “.bak”); //reopen the original config again and update it. cfg = ConfigurationManager.OpenExeConfiguration(pathToExeFile); var setting = cfg.AppSettings.Settings[keyName]; setting.Value = newValue; //save the changed configuration. cfg.Save(ConfigurationSaveMode.Full); 这种效果很好,除了一个副作用。 新保存的.config文件会丢失所有原始XML注释,但仅在AppSettings区域内。 是否可以从原始配置文件AppSettings区域保留XML注释? 如果您想快速编译并运行它,那么这是完整源代码的pastebin。

将XML字符串块附加到现有XmlDocument

我有一个已存在的XmlDocument并从文件中读取。 我想在文档中的节点上添加一块Xml。 是否有一种很好的方法来创建和添加所有节点,而不会使用许多.CreateNote和.AppendChild调用混乱我的代码? 我想要一些方法来创建一个有效的Xml部分的字符串或stringBuilder,并将其附加到XmlNode。 例如:原始XmlDoc: 并且,我想在Employee中添加一个人口统计(带有几个孩子)标签:

如何最好地在.NET中使用XPath和非常大的XML文件?

我需要在C#中对相当大的XML文件(这里可能超过一千兆字节)进行一些处理,包括执行一些复杂的xpath查询。 我遇到的问题是我通常通过System.XML库执行此操作的标准方法是在将任何内容添加到内存之前将整个文件加载到内存中,这会导致此大小的文件出现内存问题。 我只需要阅读它们并查询其中包含的数据就不需要更新文件。 一些XPath查询非常复杂,并且涉及多个级别的父子类型关系 – 我不确定这是否会影响使用流式读取器的能力,而不是将数据作为块加载到内存中。 我可以看到使其工作的一种方法是使用基于流的方法执行简单分析,并且可能将XPath语句包装到XSLT转换中,之后我可以在文件中运行,尽管看起来有点复杂。 或者我知道XPath查询有一些元素不能运行,所以我想我可以根据它的原始树结构将文档分解为一系列较小的片段,这可能足够小,可以在内存中处理造成太大的破坏。 我试图在这里解释我的目标,所以如果我在一般方法上咆哮完全错误的树,我相信大家可以让我正确…

直接/从XML Linq序列化

有没有办法取消/序列化一个对象而不绕过XmlDocument / temp字符串? 我正在寻找以下内容: class Program { static void Main(string[] args) { XDocument doc = new XDocument(); MyClass c = new MyClass(); c.SomeValue = “bar”; doc.Add(c); Console.Write(doc.ToString()); Console.ReadLine(); } } [XmlRoot(ElementName=”test”)] public class MyClass { [XmlElement(ElementName = “someValue”)] public string SomeValue { get; set; } } 我这样做时会出现错误(非空格字符无法添加到内容中。)如果我将该类包装在元素中,我看到写入的内容是 ConsoleApplication17.MyClass – 所以错误说得通。 我有自动de / serialize的扩展方法,但这不是我想要的(这是客户端,但我仍然想要更优化的东西)。 有任何想法吗?

如何防止XXE攻击(.net中的XmlDocument)

我们对代码进行了安全审核,他们提到我们的代码容易受到外部实体(XXE)攻击。 我正在使用以下代码 – string OurOutputXMLString= “00000Logince_useridce_subscribernamece_subscriberidce_groupidUnitTester9pDhE5AsKBHw85Sqgg6qdKQ==tOlkiae9epM=” XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(OurOutputXMLString); 在审计报告中,他们说它失败了,因为XML实体可以包含可以在预期的控制之外解析的URL。 XML实体解析器将尝试解析和检索外部引用。 如果可以将攻击者控制的XML提交给其中一个function,则攻击者可以访问有关内部网络,本地文件系统或其他敏感数据的信息。 为了避免这种情况,我编写了以下代码,但它不起作用。 MemoryStream stream = new MemoryStream(System.Text.Encoding.Default.GetBytes(OurOutputXMLString)); XmlReaderSettings settings = new XmlReaderSettings(); settings.DtdProcessing = DtdProcessing.Prohibit; settings.MaxCharactersFromEntities = 6000; XmlReader reader = XmlReader.Create(stream, settings); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(reader); 但我可以在这里看到,读者没有任何值加载到xmlDoc(XmlDocument)。 任何人都可以帮我错过的地方吗? 任何帮助表示赞赏!

使用REST XML Web服务

我正在尝试使用以下Web服务http://ipinfodb.com/ip_location_api.php此Web服务返回xml响应,下面的代码获取XML响应,但不知何故,当从XML响应中定相值时,它不会工作。 我的代码出了什么问题? using System; using System.Collections.Generic; using System.Text; using System.Web; using System.IO; using System.Net; using System.Xml; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { HttpWebRequest request = null; HttpWebResponse response = null; String Xml; // Create the web request request = WebRequest.Create(“http://api.ipinfodb.com/v2/ip_query.php?key=&ip=74.125.45.100&timezone=true”) as HttpWebRequest; // Get response using (response = request.GetResponse() as […]

xml更改属性的值

我在xml文件中有以下文本: 我需要更改位于LibraryDirectory LibraryDirectory=下的文本“C:\ XCAM”。 这样做的聪明方法是什么,我只是不想搜索LibraryDirectory=然后搜索第一个和最后一个“然后更改文本。”

xml序列化已保存的游戏数据

我正在尝试编写一种方法来为游戏保存一些基础数据。 目前我正在尝试以xml格式保存数据,其中父元素包含元素数组,其中包含索引,完成和尝试的字段。 这是我第一次尝试序列化c#中的任何内容,所以我遇到了一些问题,目前,当我尝试deserialize化文件时,它会因为invalidoperationexception (底部的exception细节)。 这是代码。 public void SaveData() { const string filename = “data.vision”; #if WINDOWS_PHONE IsolatedStorageFile dataFile = IsolatedStorageFile.GetUserStoreForApplication(); #else IsolatedStorageFile dataFile = IsolatedStorageFile.GetUserStoreForDomain(); #endif try { // Create an isolated storage stream and initialize it as null. IsolatedStorageFileStream isolatedFileStream = null; // Open the isolated storage stream, and write the save data file. […]

如何按属性删除XML节点

XML 此代码不会从xml中删除该节点。 你能帮我找问题吗? XmlDocument doc = new XmlDocument(); doc.Load(localXMLpath + xmlFileName); XmlNode delNode= doc.SelectSingleNode(“/WorkTable/SpecialDays/Day[@date=’24.07.2015′]”); delNode.ParentNode.RemoveChild(delNode); doc.Save(localXMLpath + xmlFileName);

自定义xml序列化

我需要帮助,我有一些数据来自xml,我想制作一个对象,用它做一些事情,序列化它并发送它…但它必须是某种自定义序列化。 xml喜欢: a oof 12 b meow 需要表现为: abstract class Animal :XmlMagic { public string Name{get;set;} public string Sound{get;set;} public void SomeMagicalXMLSerializationMethod() {} public void SomeMagicalXMLDeSerializationMethod() {} } class Dog: Animal, XmlMagic { public int SomeOtherProp{get;set;} public void SomeMagicalXMLSerializationMethod() {} public void SomeMagicalXMLDeSerializationMethod() {} }