繁体   English   中英

如何使用LINQ-to-XML将XML节点保存回XML文件?

[英]How to save XML node back into XML file with LINQ-to-XML?

我有一个XML文件,可用于创建对象,更改对象,然后将对象保存回 XML文件中。

我必须在以下代码中进行哪些更改,以便它基于id从XML中提取一个节点,将该节点替换为新节点,然后将其保存回XML中?

以下给出的“ System.Xml.Linq.XElement”不包含采用“ 0”参数的构造函数

//GET ALL SMARTFORMS AS XML
XDocument xmlDoc = null;
try
{
    xmlDoc = XDocument.Load(FullXmlDataStorePathAndFileName);
}
catch (Exception ex)
{
    HandleXmlFileNotFound(ex);
}

//EXTRACT THE NODE THAT NEEDS TO BE REPLACED
XElement oldElementToOverwrite = xmlDoc.Descendants("smartForm")
    .Where(sf => (int)sf.Element("id") == 2)
    .Select(sf => new XElement());

//CREATE THE NODE THAT WILL REPLACE IT
XElement newElementToSave = new XElement("smartForm",
                              new XElement("id", this.Id),
                              new XElement("idCode", this.IdCode),
                              new XElement("title", this.Title)
                              );

//OVERWRITE OLD WITH NEW
oldElementToOverwrite.ReplaceWith(newElementToSave);

//SAVE XML BACK TO FILE
xmlDoc.Save(FullXmlDataStorePathAndFileName);

XML档案:

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <smartForm>
    <id>1</id>
    <whenCreated>2008-12-31</whenCreated>
    <itemOwner>system</itemOwner>
    <publishStatus>published</publishStatus>
    <correctionOfId>0</correctionOfId>
    <idCode>customerSpecial</idCode>
    <title>Edit Customer Special</title>
    <description>This form has a special setup.</description>
    <labelWidth>200</labelWidth>
  </smartForm>
  <smartForm>
    <id>2</id>
    <whenCreated>2008-12-31</whenCreated>
    <itemOwner>system</itemOwner>
    <publishStatus>published</publishStatus>
    <correctionOfId>0</correctionOfId>
    <idCode>customersMain</idCode>
    <title>Edit Customer</title>
    <description>This form allows you to edit a customer.</description>
    <labelWidth>100</labelWidth>
  </smartForm>
  <smartForm>
    <id>3</id>
    <whenCreated>2008-12-31</whenCreated>
    <itemOwner>system</itemOwner>
    <publishStatus>published</publishStatus>
    <correctionOfId>0</correctionOfId>
    <idCode>customersNameOnly</idCode>
    <title>Edit Customer Name</title>
    <description>This form allows you to edit a customer's name only.</description>
    <labelWidth>100</labelWidth>
  </smartForm>
</root>

好吧,该错误与保存甚至与替换无关-它与您尝试在不指定名称的情况下创建XElement有关。 您为什么要尝试使用全Select 我的猜测是您只想使用Single

XElement oldElementToOverwrite = xmlDoc.Descendants("smartForm")
    .Where(sf => (int)sf.Element("id") == 2)
    .Single();

(正如Noldorin所指出的,您可以给Single一个谓词,以避免完全使用Where 。就我个人而言,我很想将这两个操作分开,但它们在语义上是等效的。)

这将返回序列中的单个元素,如果有0个或多个元素,则抛出异常。 替代方法是使用SingleOrDefaultFirstFirstOrDefault

  • SingleOrDefault如果合法)为0或1
  • First如果有一个或多个合法的话
  • FirstOrDefault如果合法)为0或更大

如果您使用的是“ OrDefault”,那么如果没有匹配项,结果将为null

我认为问题仅仅是您在分配oldElementToOverwrite的语句中使用Select调用。 您实际上似乎想要使用Single扩展方法。

XElement oldElementToOverwrite = xmlDoc.Descendants("smartForm")
    .Single(sf => (int)sf.Element("id") == 2)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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