xmldocument

Preserve xml formatting using XmlDocument

不羁的心 提交于 2019-12-04 12:40:41
I am using XmlDocument to work with xml How do I save my "XmlDocument" with my current formatting? Current formatting: <?xml version="1.0" encoding="utf-8"?> <root> <element></element> </root> Code: XmlDocument testDoc = new XmlDocument(); testDoc.Load(@"C:\Test.xml"); **(do reading/writing using only XmlDocument methods)** testDoc.Save(@"C:\Test.xml"); There is a similar topic: XmlDocument class is removing formatting, c#, .NET The accepted answer is PreserveWhiteSpace = true, which in reality removes all whitespaces instead of preserving them. Example: Code: XmlDocument testDoc = new

How to load all the Xml files from a folder to an XmlDocument

情到浓时终转凉″ 提交于 2019-12-04 12:37:40
With my below code, I am able to load one Xml file in XmlDocument xWorkload. XmlDocument xWorkload = new XmlDocument(); private void button1_Click(object sender, RoutedEventArgs e) { var outputxml = new StringBuilder(string.Empty); Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); dlg.FileName = "demo"; // Default file name dlg.DefaultExt = ".xml"; // Default file extension dlg.Filter = "Xml documents (.xml)|*.xml"; // Filter files by extension var result = dlg.ShowDialog(); //Opens the dialog box if (result == true) { xWorkload.Load(dlg.FileName); string Path = dlg

How to remove encoding=“UTF-8” standalone=“no” from xml Document object in Java

梦想与她 提交于 2019-12-04 04:39:18
I want to create XML in Java. DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder; docBuilder = dbfac.newDocumentBuilder(); Document doc = docBuilder.newDocument(); but Java automatically creates declaration like this <?xml version="1.0" encoding="UTF-8" standalone="no"?> How can I remove encoding="UTF-8" standalone="no" so it will be <?xml version="1.0"?> Thanks! I think there is no legal way to exclude theese attributes from generation. But after it's generated you can use XSLT to remove this. I think this is a good way. Why do you need to remove

ImportNode creates empty xmlns attribute

一笑奈何 提交于 2019-12-04 02:43:27
Regrading this code: var tmpNewNode = xdoc.ImportNode(newNode, true); if (oldNode.ParentNode != null) { oldNode.ParentNode.ReplaceChild(tmpNewNode, oldNode); return true; } tmpNewNode is created with empty xmlns attribute (xmlns=""). Any suggestion how can I avoid it? 10x What's probably happening here is that newNode comes from a document with no namespace declared, but oldNode is in a document with a namespace. In this situation, the node takes its blank namespace over to the new document and it shows up explicitly. To be honest, if it's only a problem for a string comparison, it won't hurt

Can't get XmlDocument.SelectNodes to retrieve any of my nodes?

放肆的年华 提交于 2019-12-03 18:55:25
问题 I'm trying to parse an XML document. The document in question is an AppxManifest file. An example document looks like this: <?xml version="1.0" encoding="utf-8"?> <Package xmlns="http://schemas.microsoft.com/appx/2010/manifest" xmlns:build="http://schemas.microsoft.com/developer/appx/2012/build" IgnorableNamespaces="build"> <Identity Name="uytury" Publisher="hygj" Version="1.0.0.12" ProcessorArchitecture="neutral" /> <Properties> <DisplayName>jhjj</DisplayName> <PublisherDisplayName>bhhjb<

Which is the best for performance wise: XPathNavigator with XPath vs Linq to Xml with query?

久未见 提交于 2019-12-03 16:12:40
I have an application in which I am using XPathNavigator to iterate nodes. It is working fine. But I want to know that if I use LINQ to Xml.... What benefits(Performance, maintainability) I will get? With XPath, LINQ to Xml what is the performance hit? I am using C#.net, VS 2010 and my .xml is mid size. Well, XPathNavigator will generally be faster than Linq to XML queries. But there's always 'but'. Linq to XML will definitely make your code more readable and maintainable. It's easier (at least for me) to read linq query then analyze XPath. Also - you will get intellisense when writing query

Search for nodes by name in XmlDocument

痞子三分冷 提交于 2019-12-03 10:40:01
I'm trying to find a node by name in an XmlDocument with the following code: private XmlNode FindNode(XmlNodeList list, string nodeName) { if (list.Count > 0) { foreach (XmlNode node in list) { if (node.Name.Equals(nodeName)) return node; if (node.HasChildNodes) FindNode(node.ChildNodes, nodeName); } } return null; } I call the function with: FindNode(xmlDocument.ChildNodes, "somestring"); For some reason it always returns null and I'm not really sure why. Can someone help me out with this? Change this line: if (node.HasChildNodes) FindNode(node.ChildNodes, nodeName); to: if (node

Read XML file into XmlDocument

瘦欲@ 提交于 2019-12-02 16:54:13
I am very new to C#. I have XML file (text.xml). I want to read that in XmlDocument and store the stream in string variable. Timur Sadykov Use XmlDocument.Load() method to load XML from your file. Then use XmlDocument.InnerXml property to get XML string. XmlDocument doc = new XmlDocument(); doc.Load("path to your file"); string xmlcontents = doc.InnerXml; Pupper If your .NET version is newer than 3.0 you can try using System.Xml.Linq.XDocument instead of XmlDocument . It is easier to process data with XDocument . Hope you dont mind Xml.Linq and .net3.5+ XElement ele = XElement.Load("text.xml")

Not getting Xml portion in formatted form?

﹥>﹥吖頭↗ 提交于 2019-12-02 10:46:33
问题 I am trying to create a xml file which is perfectly formatted. It include somethings Element getting replaced later on. The output is not perfectly formatted. xEvent contains a whole xml template form like string Here is the most of the relevant code string c2 = "</Caption>]]>"; string c = "<![CDATA[<Caption xmlns=\"http://www.omn.tv/iTX/OmnibusCxsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.om.t/iTX/OmnibusCn.xsd\">"; XmlDocument xml2 = new

Returning XmlDocument from WCF service not working

牧云@^-^@ 提交于 2019-12-02 10:31:50
I am trying to update some WCF service methods that return strings to return XmlDocument objects. I've tried returning it as-is and encapsulating it in a datacontract object. Either way I'm hitting an error upon attempting to update the service reference. The error suggest encapsulating it in a datacontract with an operations contract which I am doing. Is there a trick to this? There's a way to return a XmlDocument from WCF, but you need to use the XmlSerializer instead of the default serializer ( DataContractSerialier ) - the code below shows how it can be done. Having said that, do consider