how to remove node from string formatted xml using c#
-
I have an string parameter with xml content in it. Basically the string have an XML inside.
7-1623419127
2021-06-11T06:45:27-08:00
2021-06-18T10:26:37-08:00
1624037197
3
E0000-1624055316
*michael
470000-1623418914
Accounts Receivable
2021-06-11
1
*michael
false
false
2021-06-11
2021-06-11
1200.00
10000-1623418832
Vat 14%
14.00
168.00
0.00
1368.00
false
true
10000-1623418756
Tax
9-1623419127
30000-1623418915
*م1
صنف رقم 110
30.00
300.00
10000-1623418756
Tax12-1623419127
40000-1623418915
*م2
مواصفات ملابس 2
15
60.00
900.00
10000-1623418756
Tax13-1623419127
14-1623419127
60000-1624037191
total
1200.00i want to Delete
and
with out delete
*م1
-
I have an string parameter with xml content in it. Basically the string have an XML inside.
7-1623419127
2021-06-11T06:45:27-08:00
2021-06-18T10:26:37-08:00
1624037197
3
E0000-1624055316
*michael
470000-1623418914
Accounts Receivable
2021-06-11
1
*michael
false
false
2021-06-11
2021-06-11
1200.00
10000-1623418832
Vat 14%
14.00
168.00
0.00
1368.00
false
true
10000-1623418756
Tax
9-1623419127
30000-1623418915
*م1
صنف رقم 110
30.00
300.00
10000-1623418756
Tax12-1623419127
40000-1623418915
*م2
مواصفات ملابس 2
15
60.00
900.00
10000-1623418756
Tax13-1623419127
14-1623419127
60000-1624037191
total
1200.00i want to Delete
and
with out delete
*م1
-
I have an string parameter with xml content in it. Basically the string have an XML inside.
7-1623419127
2021-06-11T06:45:27-08:00
2021-06-18T10:26:37-08:00
1624037197
3
E0000-1624055316
*michael
470000-1623418914
Accounts Receivable
2021-06-11
1
*michael
false
false
2021-06-11
2021-06-11
1200.00
10000-1623418832
Vat 14%
14.00
168.00
0.00
1368.00
false
true
10000-1623418756
Tax
9-1623419127
30000-1623418915
*م1
صنف رقم 110
30.00
300.00
10000-1623418756
Tax12-1623419127
40000-1623418915
*م2
مواصفات ملابس 2
15
60.00
900.00
10000-1623418756
Tax13-1623419127
14-1623419127
60000-1624037191
total
1200.00i want to Delete
and
with out delete
*م1
The simplest solution would be to use LINQ to XML: Overview - LINQ to XML | Microsoft Docs[^]
string sourceXml = @"...";
XDocument document = XDocument.Parse(sourceXml);List<XElement> elementsToRemove = document.Descendants("ListID").ToList();
foreach (XElement element in elementsToRemove)
{
element.Remove();
}elementsToRemove = document.Descendants("ItemRef").ToList();
foreach (XElement element in elementsToRemove)
{
element.Remove();
}string resultXml = document.ToString();
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
The simplest solution would be to use LINQ to XML: Overview - LINQ to XML | Microsoft Docs[^]
string sourceXml = @"...";
XDocument document = XDocument.Parse(sourceXml);List<XElement> elementsToRemove = document.Descendants("ListID").ToList();
foreach (XElement element in elementsToRemove)
{
element.Remove();
}elementsToRemove = document.Descendants("ItemRef").ToList();
foreach (XElement element in elementsToRemove)
{
element.Remove();
}string resultXml = document.ToString();
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
thanks for your response this code remove all sub nodes in
ItemRef
but i need to keep the sub node
*م1
thanks