none
XML Looping in C# RRS feed

  • Question

  • Hi Team,

    I have below format XML. I need to loop through and change the few attribute. Example Attribute <DiscPct /> . I need to update this to <DiscPct  i:nil="true" />. Same way wherever XML attribute like<OptCD /> I need to change it to i:nil="true". Please help to implement.

    <test   xmlns="http://schemas.datacontract.org/2004/07/TEST"    xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
       <ABC>
          <name>Jack</name>
          <fname>Jack S</fname>      
       </ABC>
       <XYZ>
          <Lan>English</Lan>
          <Type>Low</Type>      
       </XYZ> 
        <Items>
            <LItem>
    <Employee>
    <EmailAddress>test@gmail.com</EmailAddress>
    <EmployeeNr i:nil="true" />
    </Employee>        
    <DiscPct />
    <OptCD/>
    </LItem>
    <LItem>
    <Employee>
    <EmailAddress>test@gmail.com</EmailAddress>
    <EmployeeNr i:nil="true" />
    </Employee>    
    <DiscPct />
    <OptCD/>
    </LItem>
        </Items>
    <BD>
            <LBD>               
             <DiscPct/>         
             <Index>0</Index>
             <BL>
                <BLN>
                  <Date i:nil="true" />
                  <Desc>Sreenath</Desc>              
                </BLN>            
             </BL>        
            </LBD>
        </BD>
    </test>

    My final XML after logic should be like this :

    <test   xmlns="http://schemas.datacontract.org/2004/07/TEST"    xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
       <ABC>
          <name>Jack</name>
          <fname>Jack S</fname>      
       </ABC>
       <XYZ>
          <Lan>English</Lan>
          <Type>Low</Type>      
       </XYZ> 
        <Items>
            <LItem>
    <Employee>
    <EmailAddress>test@gmail.com</EmailAddress>
    <EmployeeNr i:nil="true" />
    </Employee>        
    <DiscPct i:nil="true" />
    <OptCD i:nil="true" />
    </LItem>
    <LItem>
    <Employee>
    <EmailAddress>test@gmail.com</EmailAddress>
    <EmployeeNr i:nil="true" />
    </Employee>    
    <DiscPct i:nil="true" />
    <OptCD i:nil="true" />
    </LItem>
        </Items>
    <BD>
            <LBD>               
             <DiscPct i:nil="true" />        
             <Index>0</Index>
             <BL>
                <BLN>
                  <Date i:nil="true" />
                  <Desc>Sreenath</Desc>              
                </BLN>            
             </BL>        
            </LBD>
        </BD>
    </test>

    Monday, November 5, 2018 11:27 AM

Answers

  • Hi Intermediate-User,

    Here is a sample code that using LINQ to xml for your reference.

    XDocument doc = XDocument.Load("test.xml");
                
                XNamespace ns = "http://schemas.datacontract.org/2004/07/TEST";
                XNamespace nsi = @"http://www.w3.org/2001/XMLSchema-instance";
                var items = doc.Descendants(ns + "DiscPct");
                foreach (var item in items)
                {
                    item.Add(new XAttribute(nsi + "nil", "true"));
                }
                doc.Save("test1.xml");

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 6, 2018 9:23 AM
    Moderator
  • Hi, Intermediate-User,

    For your question, if you want to update <DiscPct />to <DiscPct  i:nil="true" /> and update < <OptCD /> />to < <OptCD /> i:nil="true" />, please try the code below.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace test1
    {
        class Program
        {
            static void Main(string[] args)
            {
                XmlDocument doc = new XmlDocument();
                string path = @"D:\XML\test1\first.xml";
                doc.Load(path);
                XmlNode xn = doc.SelectSingleNode("/test/Items/LItem");
                XmlElement xe = (XmlElement)doc.SelectSingleNode("/test/Items/LItem/DiscPct");
                XmlElement x2 = (XmlElement)doc.SelectSingleNode("/test/Items/LItem/OptCD");
                xn.AppendChild(xe);
                xn.AppendChild(x2);
                XmlAttribute attr = doc.CreateAttribute("i", "nil", "http://www.w3.org/2001/XMLSchema-instance");
                attr.Value = "true";
                XmlAttribute attr1 = doc.CreateAttribute("i", "nil", "http://www.w3.org/2001/XMLSchema-instance");
                attr1.Value = "true";
                xe.SetAttributeNode(attr);
                x2.SetAttributeNode(attr1);
                XmlNode xn11= doc.SelectSingleNode("/test/BD/LBD");
                XmlElement x31= (XmlElement)doc.SelectSingleNode("/test/BD/LBD/DiscPct");
                xn11.AppendChild(x31);
                XmlAttribute attr32 = doc.CreateAttribute("i", "nil", "http://www.w3.org/2001/XMLSchema-instance");
                attr32.Value = "true";
                x31.SetAttributeNode(attr32);
                doc.Save(path);
                Console.ReadKey();
            }
         
        }
    }
    

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 7, 2018 2:41 AM
    Moderator

All replies

  • Hello, 

    Please help to implement.

    How far you was able to get? 


    Sincerely, Highly skilled coding monkey.

    Monday, November 5, 2018 11:58 AM
  • Hi Intermediate-User,

    Here is a sample code that using LINQ to xml for your reference.

    XDocument doc = XDocument.Load("test.xml");
                
                XNamespace ns = "http://schemas.datacontract.org/2004/07/TEST";
                XNamespace nsi = @"http://www.w3.org/2001/XMLSchema-instance";
                var items = doc.Descendants(ns + "DiscPct");
                foreach (var item in items)
                {
                    item.Add(new XAttribute(nsi + "nil", "true"));
                }
                doc.Save("test1.xml");

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 6, 2018 9:23 AM
    Moderator
  • Hi, Intermediate-User,

    For your question, if you want to update <DiscPct />to <DiscPct  i:nil="true" /> and update < <OptCD /> />to < <OptCD /> i:nil="true" />, please try the code below.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace test1
    {
        class Program
        {
            static void Main(string[] args)
            {
                XmlDocument doc = new XmlDocument();
                string path = @"D:\XML\test1\first.xml";
                doc.Load(path);
                XmlNode xn = doc.SelectSingleNode("/test/Items/LItem");
                XmlElement xe = (XmlElement)doc.SelectSingleNode("/test/Items/LItem/DiscPct");
                XmlElement x2 = (XmlElement)doc.SelectSingleNode("/test/Items/LItem/OptCD");
                xn.AppendChild(xe);
                xn.AppendChild(x2);
                XmlAttribute attr = doc.CreateAttribute("i", "nil", "http://www.w3.org/2001/XMLSchema-instance");
                attr.Value = "true";
                XmlAttribute attr1 = doc.CreateAttribute("i", "nil", "http://www.w3.org/2001/XMLSchema-instance");
                attr1.Value = "true";
                xe.SetAttributeNode(attr);
                x2.SetAttributeNode(attr1);
                XmlNode xn11= doc.SelectSingleNode("/test/BD/LBD");
                XmlElement x31= (XmlElement)doc.SelectSingleNode("/test/BD/LBD/DiscPct");
                xn11.AppendChild(x31);
                XmlAttribute attr32 = doc.CreateAttribute("i", "nil", "http://www.w3.org/2001/XMLSchema-instance");
                attr32.Value = "true";
                x31.SetAttributeNode(attr32);
                doc.Save(path);
                Console.ReadKey();
            }
         
        }
    }
    

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 7, 2018 2:41 AM
    Moderator