It is not feasible to delete the square brackets in the datatable in this way.
dr.Field<string>("XMLNodePath")
gets a string, which has no relationship with the datatable, and the changes made to it will not affect the datatable.
Try something like this:
foreach (DataRow dr in dt.Rows)
{
string SecLegInd = "";
string XMLNodePath = "";
string XMLElementValue = "";
if (dr.Field<string>("SecLegInd") != "")
{
SecLegInd = dr.Field<string>("SecLegInd").Remove(1, 1);
SecLegInd = SecLegInd.Remove(SecLegInd.Length - 1, 1);
}
if (dr.Field<string>("XMLNodePath") != "")
{
XMLNodePath = dr.Field<string>("XMLNodePath").Remove(0, 1);
XMLNodePath = XMLNodePath.Remove(XMLNodePath.Length - 1, 1);
}
if (dr.Field<string>("XMLElementValue") != "")
{
XMLElementValue = dr.Field<string>("XMLElementValue").Remove(0, 1);
XMLElementValue = XMLElementValue.Remove(XMLElementValue.Length - 1, 1);
}
row = new Dictionary<string, string>();
row.Add(XMLNodePath, XMLElementValue);
rows.Add(row);
}
Or use Regex:
foreach (DataRow dr in dt.Rows)
{
string SecLegInd = Regex.Replace(dr.Field<string>("SecLegInd"), "[\\[\\]]", string.Empty);
string XMLNodePath = Regex.Replace(dr.Field<string>("XMLNodePath"), "[\\[\\]]", string.Empty);
string XMLElementValue = Regex.Replace(dr.Field<string>("XMLElementValue"), "[\\[\\]]", string.Empty);
dr.SetField(0, SecLegInd);
dr.SetField(1, XMLNodePath);
dr.SetField(2, XMLElementValue);
row = new Dictionary<string, string>();
row.Add(dr.Field<string>("XMLNodePath"), dr.Field<string>("XMLElementValue"));
rows.Add(row);
}
Update(6/22):
Copy the xml, and then use the functions of visual studio to construct a class that conforms to its structure.
This feature requires you to install certain workloads, such as ASP.NET and web development. To prevent you from being unable to use this feature without installing these workloads, I will provide you with the produced class as an attachment.
string strs = File.ReadAllText(@"C:\...\test-xml.xml");
XmlSerializer serializer = new XmlSerializer(typeof(envelope));
envelope result;
using (TextReader reader = new StringReader(strs))
{
result = (envelope)serializer.Deserialize(reader);
}
string JSONstring = JsonConvert.SerializeObject(result, new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore,
});
var xml = JsonConvert.DeserializeXmlNode("{\"envelope\":" + JSONstring + "}", "envelope");
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.