none
How to add element and attribute when file read by xdocument class RRS feed

  • Question

  • see how xml element & attribute which i need to add by xdocument

    BrokerTab_Id="0" Broker_Id="0" these are the attribute i need to add when i will add element called TickerBrokerDateFormatMap

    <TickerBrokerDateFormatMap BrokerTab_Id="0" Broker_Id="0" Ticker_Id="AMZN">
    
        <StandardDate>1Q 2010A</StandardDate>
    
        <ColumnCoordinate>X</ColumnCoordinate>
    
        <TickerBrokerDateFormatMaps_Id>0</TickerBrokerDateFormatMaps_Id>
    
        <BrokerDate StandardDate="1Q 2010A" Broker_Id="0" BrokerTab_Id="0">
    
          <year>1Q10</year>
    
          <Quater>1Q10</Quater>
    
        </BrokerDate>
    
      </TickerBrokerDateFormatMap>

    suppose i am reading a xml file by xdocument where i need to add the above xml element & attribute into my xml file. how it will be possible.

    before i add element in xml file by xdocument in another program like this way. here is code. in the below code i add element and update element value if exist but did not add any attribute but i need to only add element with attribute too with values. please guide me with code how i can do it with xdocument class. my first xml chunk i need to add. how to do it with xdocument class. thanks

    try
                    {
                        XDocument xmlDoc = XDocument.Load(path);
    
                        //iterate in return data from db
                        foreach (var data in returndata)
                        {
                            //query 10QK xml data based on section & lineitem
                            var items = (from item in xmlDoc.Descendants("TickerBrokerStandardDateLineitemValue")
                                         where item.Element("TabName").Value.Trim() == data.Section
                                         && item.Element("StandardLineItem").Value.Trim() == data.LineItem
                                         select item).ToList();
    
    
                            foreach (var item in items)
                            {
                                //element will be inserted or updated in xml when match found
                                if (item.Element("SectionID") == null)
                                {
                                    //if SectionID element does not exist then it will be added in xml having ID 
                                    item.Add(new XElement("SectionID", data.SectionID));
                                }
                                else
                                {
                                    //if SectionID element exist then it will be updated with db value
                                    item.Element("SectionID").SetValue(data.SectionID);
                                }
    
                                //element will be inserted or updated in xml when match found
                                if (item.Element("LineItemID") == null)
                                {
                                    //if LineItemID element does not exist then it will be added in xml having ID 
                                    item.Add(new XElement("LineItemID", data.LineItemID));
                                }
                                else
                                {
                                    //if LineItemID element exist then it will be updated with db value
                                    item.Element("LineItemID").SetValue(data.LineItemID);
                                }
                                
                                if (data.Approved == 1)
                                {
                                    //if approved then xfundcode will be updated
                                    if (item.Element("XFundCode") != null)
                                    {
                                        //if XFundCode element exist then it will be updated with db value
                                        item.Element("XFundCode").SetValue(data.ApprovedXFundCode);
                                    }
                                }
                                else if (data.Approved == 0)
                                {
                                    //if unapproved then xfundcode will be empty
                                    if (item.Element("XFundCode") != null)
                                    {
                                        //if XFundCode element exist then it will be updated with db value
                                        item.Element("XFundCode").SetValue(string.Empty);
                                    }
                                }
                            }
                        }
    
                        xmlDoc.Save(path);
    
                        oResponseContext.IsSuccess = true;
                        oResponseContext.Message = "10QK Bogey file successfully updated";
                        oResponseContext.ResponseData = path;
                    }
                    catch(Exception ex)
                    {
                        oResponseContext.IsSuccess = false;
                        oResponseContext.Message = "Error "+ex.Message.ToString();
                        oResponseContext.ResponseData = null;
                    }
                }

    Saturday, November 16, 2019 8:08 PM

Answers

  • Hi Sudip_inn, 

    Thank you for posting here.

    For your question, you want to know how to use XDocument to add xml element and attribute.

    I create a xml and make a test on my side.

    You can refer to the following code.

                string xmlPath = @"path";
                XDocument XDoc = XDocument.Load(xmlPath);
                var items = (from item in XDoc.Descendants("node1")
                             select item).ToList();
                foreach (var item in items)
                {
                    if (item.Element("childNode1") == null)
                    {
                        item.Add(new XElement("childNode1", 1, new XAttribute("attribute1", "value1")));
                    }
                    else
                    {         
                        item.Element("childNode1").SetValue(1);
                        XAttribute attribute1 = new XAttribute("attribute1", "value1");
                        item.Element("childNode1").Add(attribute1);
                    }
                    if (item.Element("childNode2") == null)
                    {
                        item.Add(new XElement("childNode2", 2,new XAttribute("attribute2", "value2")));
                    }
                    else
                    {
                        item.Element("childNode2").SetValue(2);
                        XAttribute attribute2 = new XAttribute("attribute2", "value2");
                        item.Element("childNode2").Add(attribute2);
                    }
                    XDoc.Save(xmlPath);
                }

    My original xml file.

    Result of my test.

    Hope it can help you.

    Best Regards,

    Xingyu Zhao



    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.

    • Marked as answer by Sudip_inn Monday, November 18, 2019 7:12 AM
    Monday, November 18, 2019 6:41 AM
    Moderator