将模型数据保存为XML

本文关键字:XML 保存 数据 模型 | 更新日期: 2023-09-27 17:50:48

我的xml是:

<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>

我想检查section name n audio path是否相等我的代码是:

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仍然和以前一样…数据保存在模型中。我想将数据保存到XML中。

将模型数据保存为XML

如果你想修改XML,为什么不简单地返回XElement s而不是Text对象呢?

.....
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();
.....