XMLNS repetition while appending child - c#

I am trying to automate standalone.xml of JBoss-AS-7.1.1 from another module of my project.
Following is the part of XML im trying to update
</subsystem>
<subsystem xmlns="urn:jboss:domain:configadmin:1.0" />
<subsystem xmlns="urn:jboss:domain:datasources:1.0">
<datasources>
<datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
<connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1</connection-url>
<driver>h2</driver>
<security>
<user-name>sa</user-name>
<password>sa</password>
</security>
</datasource>
<drivers>
<driver name="h2" module="com.h2database.h2">
<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
</driver>
<driver name="mysql" module="com.mysql.jdbc">
<xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
</driver>
</drivers>
</datasources>
</subsystem>
<subsystem xmlns="urn:jboss:domain:deployment-scanner:1.1">
After updating correctly it would look like
<subsystem xmlns="urn:jboss:domain:configadmin:1.0" />
<subsystem xmlns="urn:jboss:domain:datasources:1.0">
<datasources>
<datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
<connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1</connection-url>
<driver>h2</driver>
<security>
<user-name>sa</user-name>
<password>sa</password>
</security>
</datasource>
<drivers>
<driver name="h2" module="com.h2database.h2">
<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
</driver>
<driver name="mysql" module="com.mysql.jdbc">
<xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
</driver>
</drivers>
<datasource jndi-name="java:/session-tracking-dataSource-orcl" pool-name="session-tracking-dataSource-orcl" enabled="true" use-java-context="true">
<connection-url>jdbc:mysql://127.0.0.1:3306//fusion?useUnicode=true&amp;characterEncoding=UTF-8&amp;</connection-url>
<connection-property name="charSet">UTF-8</connection-property>
<driver>mysql</driver>
<pool>
<max-pool-size>10</max-pool-size>
<min-pool-size>1</min-pool-size>
</pool>
<security>
<user-name>root</user-name>
<password>root</password>
</security>
<timeout>
<idle-timeout-minutes>1</idle-timeout-minutes>
<query-timeout>5</query-timeout>
</timeout>
</datasource>
</datasources>
</subsystem>
<subsystem xmlns="urn:jboss:domain:deployment-scanner:1.1">
So here goes my piece of code.
XMLObj.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace XMLOpsHelper
{
class XMLObj
{
private string name;
private List<Object> value;
private Dictionary<string, string> attr;
private XMLContext context;
public XMLObj(string name, XMLContext context)
{
this.name = name;
this.context = context;
value = new List<Object>();
attr = new Dictionary<string, string>();
}
public void addAttribute(string key, string value)
{
this.attr.Add(key, value);
}
public void assignValue(Object val)
{
if ((val.GetType() == typeof(String)) || (val.GetType() == typeof(string)) || (val.GetType() == typeof(XMLObj)))
{
this.value.Add(val);
}
}
// for removing empty xmlns
public Object _toString()
{
XmlNode newNode = context.Doc.CreateNode(XmlNodeType.Element, this.name, context.XmlNS);
foreach (KeyValuePair<string, string> entry in this.attr)
{
XmlAttribute attr = context.Doc.CreateAttribute(entry.Key);
attr.Value=entry.Value;
newNode.Attributes.Append(attr);
}
foreach (Object item in this.value)
{
if (item is string)
{
newNode.InnerText=(string)item;
}
if (item is XMLObj)
{
Object nextItem = ((XMLObj)item)._toString();
if (nextItem is XmlNode)
{
newNode.AppendChild((XmlNode)nextItem);
}
}
}
return newNode;
}
}
}
Little background on the XMLObj class: For every tag new instance this class will be created. If the appended innerXML/innerText will go inside value list. The _toString (temporary name) method to be called later on the root object to translate it to tags.
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace XMLOpsHelper
{
class XMLUpdater
{
XmlDocument doc = new XmlDocument();
XmlNode root;
public XMLUpdater(string xmlStr) {
doc.LoadXml(xmlStr);
}
public XMLUpdater(XmlNode xmlNode)
{
doc.LoadXml(xmlNode.OuterXml);
}
public XMLUpdater(XmlNode xmlNode, XmlNode root)
{
doc.LoadXml(xmlNode.OuterXml);
this.root = root;
}
public void appendChild(XmlNode parentNode){
XmlNode importNode = root.OwnerDocument.ImportNode(doc.DocumentElement, true);
parentNode.AppendChild(importNode);
}
public XmlDocument getDoc() {
return this.doc;
}
}
}
Once XMLObject hierarchy is created and translated to tags XMLUpdater place it in appropriate position by calling appendChild.
And the end result is
<subsystem xmlns="urn:jboss:domain:configadmin:1.0" />
<subsystem xmlns="urn:jboss:domain:datasources:1.0">
<datasources>
<datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
<connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1</connection-url>
<driver>h2</driver>
<security>
<user-name>sa</user-name>
<password>sa</password>
</security>
</datasource>
<drivers>
<driver name="h2" module="com.h2database.h2">
<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
</driver>
<driver name="mysql" module="com.mysql.jdbc">
<xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
</driver>
</drivers>
<datasource jndi-name="java:/session-tracking-dataSource-orcl" pool-name="session-tracking-dataSource-orcl" enabled="true" use-java-context="true" xmlns="urn:jboss:domain:datasources:1.0">
<connection-url>jdbc:mysql://127.0.0.1:3306//fusion?useUnicode=true&amp;characterEncoding=UTF-8&amp;</connection-url>
<connection-property name="charSet">UTF-8</connection-property>
<driver>mysql</driver>
<pool>
<max-pool-size>10</max-pool-size>
<min-pool-size>1</min-pool-size>
</pool>
<security>
<user-name>root</user-name>
<password>root</password>
</security>
<timeout>
<idle-timeout-minutes>1</idle-timeout-minutes>
<query-timeout>5</query-timeout>
</timeout>
</datasource>
</datasources>
</subsystem>
<subsystem xmlns="urn:jboss:domain:deployment-scanner:1.1">
XMLNS is getting appended at the end of the following line which is not expected since subsystem already has it
<datasource jndi-name="java:/session-tracking-dataSource-orcl" pool-name="session-tracking-dataSource-orcl" enabled="true" use-java-context="true" xmlns="urn:jboss:domain:datasources:1.0">
Any idea what i am doing wrong?

Related

how to class wrap a list in c# for xml serializing

How can one class wrap a list in c# for xml serializing?
I would like to add a root. maybe a class wrapper is not a good idea. And should I use a different aprouch?
When I serialize the following class:
public class Parts
{
//main class
[XmlElement("Access")]
public List<Access> AccessDB = new List<Access>
{
new Access
{
Items = new[] {
new Component { Name = "dbName" }
,new Component { Name = "DbElement" } }
, Scope = "GlobalVariable", UId = "21"
},
new Access
{
Items = new[] {
new Component { Name = "TagName" } }
, Scope = "GlobalVariable", UId = "22"
}
};
}
I get:
<Parts>
<Access Scope="Scope" UId="21">
<Symbol>
<Component Name="Name" />
<Component Name="Name" />
</Symbol>
</Access>
<Access Scope="Scope" UId="22">
<Symbol>
<Component Name="Name" />
</Symbol>
</Access>
<Part Name="PartName" UId="23" />
</Parts>
but what I need is:
<myroot>
<Parts>
<Access Scope="Scope" UId="21">
<Symbol>
<Component Name="Name" />
<Component Name="Name" />
</Symbol>
</Access>
<Access Scope="Scope" UId="22">
<Symbol>
<Component Name="Name" />
</Symbol>
</Access>
<Part Name="PartName" UId="23" />
</Parts>
</myroot>
any advice is very welcome!
If the myroot element is only required to be present in the xml output, you can add it during the serialization.
Use an XmlWriter as output target for the serialization.
Before serializing the Parts instance, you use the XmlWriter to create the myroot element.
XmlWriterSettings settings = new XmlWriterSettings { Indent = true };
StringBuilder stringBuilder = new StringBuilder();
using (var xmlWriter = XmlWriter.Create(stringBuilder, settings))
{
xmlWriter.WriteStartElement("myroot"); // Writes <myroot>
var serializer = new XmlSerializer(typeof(Parts));
var parts = new Parts();
serializer.Serialize(xmlWriter, parts);
xmlWriter.WriteEndElement(); // Writes </myroot>
}
I found that it is also possible to class wrap the other classes, but the instance of the class must be public. In code:
public class myRoot
{
public Parts Parts = new Parts();
}
and then serialize the class myRoot

C# convert JSON object to XML dynamically

I'm attempting to convert JSON sent from a web browser client to the server so I can send the data as parameters in the form of XML to an SQL database. I'm struggling to do this as I have objects inside of objects and I'm not sure how to convert them dynamically into a structured XML format. Below are examples of the JSON I am using, the possible XML formats I'm trying to turn it into (or anything close to them), and the code that I am using.
JSON:
[
{"value":50,"name":"desired_gross_margin","type":"int"},
{"value":50,"name":"desired_adjusted_gross_margin","type":"int"},
{"value":0,"name":"target_electricity_tariff_unit_charge","type":"decimal"},
{"value":0,"name":"target_electricity_tariff_standing_charge","type":"decimal"},
{"value":0,"name":"target_gas_tariff_unit_charge","type":"decimal"},
{"value":0,"name":"target_gas_tariff_standing_charge","type":"decimal"},
{"value":"10/10/2016","name":"planned_go_live_date","type":"DateTime"},
{"value":"0","name":"assumed_fuel_ratio","type":"int"},
{"value":{
"year_one":"Cold",
"year_two":"Average",
"year_three":"Warm"
},
"name":"weather_variable","type":"string"}
]
Possible XML outputs:
1:
<Filters>
<CustomerParameters>
<CustomParameter name="desired_gross_margin" type="int" value="50"/>
<CustomParameter name="desired_adjusted_gross_margin" type="int" value="50"/>
<CustomParameter name="target_electricity_tariff_unit_charge" type="decimal" value="0"/>
<CustomParameter name="target_electricity_tariff_standing_charge" type="decimal" value="0"/>
<CustomParameter name="target_gas_tariff_unit_charge" type="decimal" value="0"/>
<CustomParameter name="target_gas_tariff_standing_charge" type="decimal" value="0"/>
<CustomParameter name="planned_go_live_date" type="DateTime" value="10/10/2016"/>
<CustomParameter name="assumed_fuel_ratio" type="int" value="0"/>
<CustomParamaters name="weather_variables">
<CustomParameter name="year_one" type="string" value="Cold"/>
<CustomParameter name="year_two" type="string" value="Average"/>
<CustomParameter name="year_three" type="string" value="Cold"/>
</CustomParameters>
</CustomParameters>
</Filters>
2:
<?xml version="1.0" encoding="UTF-8" ?>
<0>
<value>50</value>
<name>desired_gross_margin</name>
<type>int</type>
</0>
<1>
<value>50</value>
<name>desired_adjusted_gross_margin</name>
<type>int</type>
</1>
<2>
<value>0</value>
<name>target_electricity_tariff_unit_charge</name>
<type>decimal</type>
</2>
<3>
<value>0</value>
<name>target_electricity_tariff_standing_charge</name>
<type>decimal</type>
</3>
<4>
<value>0</value>
<name>target_gas_tariff_unit_charge</name>
<type>decimal</type>
</4>
<5>
<value>0</value>
<name>target_gas_tariff_standing_charge</name>
<type>decimal</type>
</5>
<6>
<value>10/10/2016</value>
<name>planned_go_live_date</name>
<type>DateTime</type>
</6>
<7>
<value>0</value>
<name>assumed_fuel_ratio</name>
<type>int</type>
</7>
<8>
<value>
<year_one>Cold</year_one>
<year_two>Average</year_two>
<year_three>Warm</year_three>
</value>
<name>weather_variable</name>
<type>string</type>
</8>
</xml>
C# code:
ForecastController.cs:
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web.Http;
using System.Web.Script.Serialization;
using System.Xml;
namespace ForecastServices.Controllers
{
public class ForecastController : ApiController
{
[HttpPost]
public List<Data> GetData(HttpRequestMessage request)
{
string connection_string = ConfigurationManager.ConnectionStrings["Database"].ConnectionString;
string sql = "DataHub.get_GrossMarginModel";
string json = request.Content.ReadAsStringAsync().Result;
//var filters2 = new JavaScriptSerializer().Deserialize<dynamic>(json); //this works but I can't turn it into XML! :(
List<Filter> filters = new JavaScriptSerializer().Deserialize<List<Filter>>(json);
string xml = Filter.getFilterListXML(filters);
List<Data> data = new List<Data>();
using(SqlConnection connection = new SqlConnection(connection_string))
{
connection.Open();
SqlCommand cmd = connection.CreateCommand();
cmd.CommandText = sql;
cmd.Parameters.Add(new SqlParameter("filter_xml", ""));
cmd.CommandType = CommandType.StoredProcedure;
var adapter = new SqlDataAdapter(cmd);
var set = new DataSet();
cmd.ExecuteNonQuery();
adapter.Fill(set);
if (set.Tables.Count > 0)
{
foreach (DataRow tableRow in set.Tables[0].Rows)
{
data.Add(new Data()
{
name = tableRow.ItemArray[0].ToString(),
year_one = (int)tableRow.ItemArray[1],
year_two = (int)tableRow.ItemArray[2],
year_three = (int)tableRow.ItemArray[3],
});
}
}
connection.Close();
}
return data;
}
}
}
Filter.cs:
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
namespace ForecastServices.Domain
{
public class Filter
{
public string value { get; set; }
public string name { get; set; }
public string type { get; set; }
public string getXML()
{
return string.Format("<CustomParameter name=\"{0}\" type=\"{1}\" value=\"{2}\"/>", name, type, value);
}
public static string getFilterListXML(ICollection<Filter> filters)
{
StringBuilder XMLString = new StringBuilder();
XMLString.Append("<Filters><CustomerParameters>");
foreach (Filter f in filters)
{
XMLString.Append(f.getXML());
}
XMLString.Append("</CustomParameters></Filters>");
return XMLString.ToString();
}
}
}
Not building on your current code, you can take a look at this question
Using this one-liner:
XmlDocument doc = JsonConvert.DeserializeXmlNode("{\"Row\":" + json + "}", "root"); // JSON needs to be an object
You can end up with:
<root>
<Row>
<value>50</value>
<name>desired_gross_margin</name>
<type>int</type>
</Row>
<Row>
<value>50</value>
<name>desired_adjusted_gross_margin</name>
<type>int</type>
</Row>
<Row>
<value>0</value>
<name>target_electricity_tariff_unit_charge</name>
<type>decimal</type>
</Row>
<Row>
<value>0</value>
<name>target_electricity_tariff_standing_charge</name>
<type>decimal</type>
</Row>
<Row>
<value>0</value>
<name>target_gas_tariff_unit_charge</name>
<type>decimal</type>
</Row>
<Row>
<value>0</value>
<name>target_gas_tariff_standing_charge</name>
<type>decimal</type>
</Row>
<Row>
<value>10/10/2016</value>
<name>planned_go_live_date</name>
<type>DateTime</type>
</Row>
<Row>
<value>0</value>
<name>assumed_fuel_ratio</name>
<type>int</type>
</Row>
<Row>
<value>
<year_one>Cold</year_one>
<year_two>Average</year_two>
<year_three>Warm</year_three>
</value>
<name>weather_variable</name>
<type>string</type>
</Row>
</root>
Where json is the input JSON on your question, it's close to what you want (which your post says is sort of okay), but if you want to go for your 1st and 2nd options, you can simply build new XML by manipulating this (renaming the nodes, etc.).

Uncommenting fragment of XML with Xml Document in C#

What is the easiest way of uncommenting the body of some node in XML? The elements have unique name, the structure of the documents look as follows:
somefile.xml
<?xml version="1.0"?>
<name1>
<irrelevant1>
<irrelevant2>
<!--
<irrelevant3 />
-->
</irrelevant2>
</irrelevant1>
<name2>
<name3>
<!--
<name4 field="The" />
<name4 field="Owls" />
<name4 field="Are />
<name4 field="Not" />
<name4 field="What" />
<name4 field="They" />
<name4 field="Seem />
-->
</name3>
</name2>
</name1>
The goal should look like this, with comments removed:
uncommented.xml
<?xml version="1.0"?>
<name1>
<irrelevant1>
<irrelevant2>
<!--
<irrelevant3 />
-->
</irrelevant2>
</irrelevant1>
<name2>
<name3>
<name4 field="The" />
<name4 field="Owls" />
<name4 field="Are />
<name4 field="Not" />
<name4 field="What" />
<name4 field="They" />
<name4 field="Seem />
</name3>
</name2>
</name1>
My approach to parsing:
XmlDocument xdoc = new XmlDocument();
xdoc.Load(#"C:\somefile.xml");
XmlNodeList nl = xdoc.GetElementsByTagName("name2");
XmlNode xn = nl[0];
string xn_content = xn.InnerXml;
xn_content = Regex.Replace(xn_content, "<!--|-->", String.Empty);
XmlDocument doc = new XmlDocument();
doc.LoadXml(xn_content);
XmlNode newNode = doc.DocumentElement;
// this import doesn't really help
xdoc.ImportNode(newNode, true);
xn.RemoveAll();
xn.AppendChild(newNode);
xdoc.Save(#"C:\uncommented.xml");
Results with the ArgumentException:
{"The node to be inserted is from a different document context."}
Your immediate problem is that you call XmlDocument.ImportNode() but do not use the returned node. You need to do newNode = xDoc.ImportNode(newNode, true);.
However, a cleaner way to do this would be to avoid the Regex parsing entirely. Instead, descend the XmlNode hierarchy, pick out the XmlComment nodes you wish to uncomment, load their InnerText into an XmlDocumentFragment, then add its newly created children to the parent node of the comment:
public static class XmlNodeExtensions
{
public static XmlDocument Document(this XmlNode node)
{
for (; node != null; node = node.ParentNode)
{
var doc = node as XmlDocument;
if (doc != null)
return doc;
}
return null;
}
public static IEnumerable<XmlNode> AncestorsAndSelf(this XmlNode node)
{
for (; node != null; node = node.ParentNode)
yield return node;
}
public static IEnumerable<XmlNode> DescendantsAndSelf(this XmlNode root)
{
if (root == null)
yield break;
yield return root;
foreach (var child in root.ChildNodes.Cast<XmlNode>())
foreach (var subChild in child.DescendantsAndSelf())
yield return subChild;
}
public static void UncommentXmlNodes(IEnumerable<XmlComment> comments)
{
foreach (var comment in comments.ToList())
UncommentXmlNode(comment);
}
public static void UncommentXmlNode(XmlComment comment)
{
if (comment == null)
throw new NullReferenceException();
var doc = comment.Document();
if (doc == null)
throw new InvalidOperationException();
var parent = comment.ParentNode;
var innerText = comment.InnerText;
XmlDocumentFragment docFrag = doc.CreateDocumentFragment();
//Set the contents of the document fragment.
docFrag.InnerXml = innerText;
XmlNode insertAfter = comment;
foreach (var child in docFrag.ChildNodes.OfType<XmlElement>().ToList())
{
insertAfter = parent.InsertAfter(child, insertAfter);
}
parent.RemoveChild(comment);
}
}
Then call it like:
string xml = #"<?xml version=""1.0""?>
<name1>
<irrelevant1>
<irrelevant2>
<!--
<irrelevant3 />
-->
</irrelevant2>
</irrelevant1>
<name2>
<name3>
<!--
<name4 field=""The"" />
<name4 field=""Owls"" />
<name4 field=""Are"" />
<name4 field=""Not"" />
<name4 field=""What"" />
<name4 field=""They"" />
<name4 field=""Seem"" />
-->
</name3>
</name2>
</name1>
";
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
Debug.WriteLine(xmlDoc.ToXml());
XmlNodeExtensions.UncommentXmlNodes(xmlDoc.DocumentElement.DescendantsAndSelf().OfType<XmlComment>().Where(c => c.ParentNode.Name == "name3"));
Debug.WriteLine(xmlDoc.ToXml());
Note that your commented XML is invalid. <name4 field="Are /> should be <name4 field="Are"/> and <name4 field="Seem /> should be <name4 field="Seem"/>. I fixed that for you in the test case since I assume it's a typo.

Update nested value in XML

I'm trying to write a routine that updates values in an XML file and believe I'm close. Here's an example of the XML:
<?xml version="1.0" encoding="utf-8"?>
<!-- This file is generated by the GPS_Client program. -->
<Sites>
<Site>
<Id>A</Id>
<add key="landingName" value="Somewhere" />
<add key="landingLat" value="47.423719" />
<add key="landingLon" value="-123.011364" />
<add key="landingAlt" value="36" />
</Site>
<Site>
<Id>B</Id>
<add key="landingName" value="Somewhere Else" />
<add key="landingLat" value="45.629160" />
<add key="landingLon" value="-128.882934" />
<add key="landingAlt" value="327" />
</Site>
</Sites>
The key is that I need to update a specific key without updating the rest of the keys with the same name. Here's the current code:
private void UpdateOrCreateAppSetting(string filename, string site, string key, string value)
{
string path = "\"#" + filename + "\"";
XDocument doc = XDocument.Load(path);
var list = from appNode in doc.Descendants("appSettings").Elements()
where appNode.Attribute("key").Value == key
select appNode;
var e = list.FirstOrDefault();
// If the element doesn't exist, create it
if (e == null) {
new XElement(site,
new XAttribute(key, value));
// If the element exists, just change its value
} else {
e.Element(key).Value = value;
e.Attribute("value").SetValue(value);
}
}
I assume I need to concatenate site, Id and key in some way, but don't see how it's done.
Using this XmlLib, you can have it create the node if it doesn't exist automatically.
private void UpdateOrCreateAppSetting(string filename, string site,
string key, string value)
{
string path = "\"#" + filename + "\"";
XDocument doc = XDocument.Load(path);
XPathString xpath = new XPathString("//Site[Id={0}]/add[#key={1}]", site, key);
XElement node = doc.Root.XPathElement(xpath, true); // true = create if not found
node.Set("value", value, true); // set as attribute, creates attribute if not found
doc.Save(filename);
}

XDocument create XElement with XML comment

I've been trying to select some XML comments like so:
XDocument doc = XDocument.Load(args[0]);
var comments = from node in doc.Elements().DescendantNodesAndSelf()
where node.NodeType == XmlNodeType.Comment
select node as XComment;
With this solution I'm getting all xml comment of the file, but I want to select only those comments and create XElement with it:
<Connections>
...
<!-- START Individual Account Authentication -->
<!--<authentication mode="None"/>
<roleManager enabled="false"/>
<profile enabled="false"/>-->
<!-- END Individual Account Authentication -->
...
</Connections>
Any solutions ? :S
Here is a sample:
XDocument doc = XDocument.Load("input.xml");
foreach (XComment start in doc.DescendantNodes().OfType<XComment>().Where(c => c.Value.StartsWith(" START")).ToList())
{
XComment end = start.NodesAfterSelf().OfType<XComment>().FirstOrDefault(c => c.Value.StartsWith(" END"));
if (end != null)
{
foreach (XComment comment in end.NodesBeforeSelf().OfType<XComment>().Intersect(start.NodesAfterSelf().OfType<XComment>()).ToList())
{
comment.ReplaceWith(XElement.Parse("<dummy>" + comment.Value + "</dummy>").Nodes());
}
// if wanted/needed
start.Remove();
end.Remove();
}
}
doc.Save("output.xml");
That gives me
<Connections>
...
<authentication mode="None" /><roleManager enabled="false" /><profile enabled="false" />
...
</Connections>

Categories

Resources