xmldocument

XML Document to String

≡放荡痞女 提交于 2019-11-26 07:25:47
问题 What\'s the simplest way to get the String representation of a XML Document ( org.w3c.dom.Document )? That is all nodes will be on a single line. As an example, from <root> <a>trge</a> <b>156</b> </root> (this is only a tree representation, in my code it\'s a org.w3c.dom.Document object, so I can\'t treat it as a String) to \"<root> <a>trge</a> <b>156</b> </root>\" Thanks! 回答1: Assuming doc is your instance of org.w3c.dom.Document : TransformerFactory tf = TransformerFactory.newInstance();

How to prevent blank xmlns attributes in output from .NET&#39;s XmlDocument?

独自空忆成欢 提交于 2019-11-26 06:35:15
问题 When generating XML from XmlDocument in .NET, a blank xmlns attribute appears the first time an element without an associated namespace is inserted; how can this be prevented? Example: XmlDocument xml = new XmlDocument(); xml.AppendChild(xml.CreateElement(\"root\", \"whatever:name-space-1.0\")); xml.DocumentElement.AppendChild(xml.CreateElement(\"loner\")); Console.WriteLine(xml.OuterXml); Output: <root xmlns=\"whatever:name-space-1.0\"><loner xmlns=\"\" /></root> Desired Output: <root xmlns=

Removing nodes from an XmlDocument

余生长醉 提交于 2019-11-26 05:58:45
问题 The following code should find the appropriate project tag and remove it from the XmlDocument, however when I test it, it says: The node to be removed is not a child of this node. Does anyone know the proper way to do this? public void DeleteProject (string projectName) { string ccConfigPath = ConfigurationManager.AppSettings[\"ConfigPath\"]; XmlDocument configDoc = new XmlDocument(); configDoc.Load(ccConfigPath); XmlNodeList projectNodes = configDoc.GetElementsByTagName(\"project\"); for

Getting specified Node values from XML document

杀马特。学长 韩版系。学妹 提交于 2019-11-26 04:25:54
问题 I have a problem going through an XML document (with C#) and get all the necessary values. I successfully go through all specified XmlNodeLists in the XML document, successfully get all XmlNode values inside, but I have to get some values outside of this XmlNodeList. For example: <?xml version=\"1.0\" encoding=\"UTF-8\" ?> <Element xsi:schemaLocation=\"http://localhost/AML/CaseInvestigationMangement/Moduli/XmlImportControls/xsdBorrow.xsd xsd2009027_kor21.xsd\" Kod=\"370\" xmlns=\"http:/

How to change XML Attribute

℡╲_俬逩灬. 提交于 2019-11-26 02:57:00
问题 How can I change an attribute of an element in an XML file, using C#? 回答1: Mike; Everytime I need to modify an XML document I work it this way: //Here is the variable with which you assign a new value to the attribute string newValue = string.Empty; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlFile); XmlNode node = xmlDoc.SelectSingleNode("Root/Node/Element"); node.Attributes[0].Value = newValue; xmlDoc.Save(xmlFile); //xmlFile is the path of your file to be modified I hope you find

What is the simplest way to get indented XML with line breaks from XmlDocument?

荒凉一梦 提交于 2019-11-26 02:50:39
问题 When I build XML up from scratch with XmlDocument , the OuterXml property already has everything nicely indented with line breaks. However, if I call LoadXml on some very \"compressed\" XML (no line breaks or indention) then the output of OuterXml stays that way. So ... What is the simplest way to get beautified XML output from an instance of XmlDocument ? 回答1: Based on the other answers, I looked into XmlTextWriter and came up with the following helper method: static public string Beautify

How would you compare two XML Documents?

廉价感情. 提交于 2019-11-26 02:19:30
问题 As part of the base class for some extensive unit testing, I am writing a helper function which recursively compares the nodes of one XmlDocument object to another in C# (.NET). Some requirements of this: The first document is the source , e.g. what I want the XML document to look like. Thus the second is the one I want to find differences in and it must not contain extra nodes not in the first document. Must throw an exception when too many significant differences are found, and it should be

Read XML Attribute using XmlDocument

可紊 提交于 2019-11-26 00:54:23
问题 How can I read an XML attribute using C#\'s XmlDocument? I have an XML file which looks somewhat like this: <?xml version=\"1.0\" encoding=\"utf-8\" ?> <MyConfiguration xmlns=\"http://tempuri.org/myOwnSchema.xsd\" SuperNumber=\"1\" SuperString=\"whipcream\"> <Other stuff /> </MyConfiguration> How would I read the XML attributes SuperNumber and SuperString? Currently I\'m using XmlDocument, and I get the values in between using XmlDocument\'s GetElementsByTagName() and that works really well.

XmlDocument.SelectSingleNode and xmlNamespace issue

北慕城南 提交于 2019-11-26 00:44:08
问题 I\'m loading a string to an XML document that contains the following structure : <?xml version=\"1.0\" encoding=\"utf-8\"?> <Project ToolsVersion=\"4.0\" DefaultTargets=\"Build\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\"> <ItemGroup> <Compile Include=\"clsWorker.cs\" /> </ItemGroup> </Project> then im loading all into xmldocument : XmlDocument xmldoc = new XmlDocument(); xmldoc.LoadXml(Xml); then the following problem occurs : XmlNode Node = xmldoc.SelectSingleNode(\"/

XDocument or XmlDocument

浪尽此生 提交于 2019-11-25 22:34:13
问题 I am now learning XmlDocument but I\'ve just ran into XDocument and when I try to search the difference or benefits of them I can\'t find something useful, could you please tell me why you would use one over another ? 回答1: If you're using .NET version 3.0 or lower, you have to use XmlDocument aka the classic DOM API. Likewise you'll find there are some other APIs which will expect this. If you get the choice, however, I would thoroughly recommend using XDocument aka LINQ to XML. It's much