简体   繁体   中英

Save model data to XML

My xml is :

<Texts>
  <Text group="Front0" audio="Front0_1.wav"  datetime="">blank</Text>
  <Text group="Keys0" audio="Keys0_2.wav"  datetime="">blank</Text>
  <Text group="Bedroom0" section="Ceiling" audio="Bedroom0_1.wav"  datetime="26-05-2015 03:57">[[Ceiling]] </Text>
  <Text group="Bedroom0" section="Ceiling" audio="Bedroom0_1.wav" datetime="26-05-2015 03:57">[[Ceiling]] </Text>
</Texts>

And I want to check whether section name n audio path is equal or not my code is :

public Boolean setSectionContent(decimal Trans_ID, decimal Job_ID, string SectionName, string Content, string audioPath)
{
    string data;
    GroupData p = new GroupData();
    try
    {
        var gp = (from Trans_Mast in r2ge.Transcription_Master where Trans_Mast.Transcription_Id == Trans_ID && Trans_Mast.Entity_Id == Job_ID select Trans_Mast).Distinct();
        foreach (var g in gp)
        {
            p.text = g.Text_xml.ToString();

            XDocument doc = XDocument.Parse(p.text);

            var txt = doc.Descendants("Text")
                  .Where(e => (string)e.Attribute("section") == SectionName)
                  .Select(e => new Text
                  {
                      LastModifiedOn = (string)e.Attribute("datetime"),
                      Content = (string)e.Value
                  }).Distinct().ToList();
            foreach (var item in txt)
            {
                item.Content = Content;
                item.LastModifiedOn = DateTime.Now.ToString("dd-MM-yyyy hh:mm");
            }

            data = doc.ToString();

.. doc is still same as previous .. that data is saved in model..I want to save that data into XML..

Why don't simply return XElement s instead of Text objects if you want to modify the XML :

.....

var txt = doc.Descendants("Text")
             .Where(e => (string)e.Attribute("section") == SectionName
                                  && (string)e.Attribute("audio") == audioPath);

foreach (var item in txt)
{
    item.Attribute("datetime").Value = DateTime.Now.ToString("dd-MM-yyyy hh:mm");
    item.Value = Content;
}

data = doc.ToString();

.....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM