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

  • Thread starter Thread starter Sudip_inn
  • Start date Start date
S

Sudip_inn

Guest
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;
}
}

Continue reading...
 
Back
Top