简体   繁体   中英

Copy an Xml Attribute with Linq

I have following xml structure with namespaces:

<office:document-content
<office:body>
<office:text text:use-soft-page-breaks="true">
  <text:p text:style-name="Standard">&lt;Text&gt;</text:p>
</office:text>
</office:body>
</office:document-content>

This comes from the content.xml of an unzipped .odt writer file. Now I just want to copy the attribute with the inner text "<Text>" and replace the copy with a new text. I tried this:

    XmlFileOperations xml = new XmlFileOperations();
        XDocument doc = XDocument.Load(Path.Combine(ConfigManager.InputPath, "File", "content.xml"));

        var source = doc.Descendants()
            .Where(e => e.Value == "<Text>")
            .FirstOrDefault();
        var target = new XElement(source);
        target.Add(new XAttribute("Standard", source.Attribute(textLine)));

        doc.Save(Path.Combine(ConfigManager.InputPath, "File", "content.xml"));

This does not work. It tells me that I have a sign in the text which can not be applied to the name. How I can just copy my attribute in this case?

Thank you!

Edit: The result should be

<office:document-content
<office:body>
<office:text text:use-soft-page-breaks="true">
  <text:p text:style-name="Standard">&lt;Text&gt;</text:p>
  <text:p text:style-name="Standard">some new value</text:p>
</office:text>
</office:body>
</office:document-content>

If I understand you correctly, you need the value of <Text> replace with textLine .

Try this code

var source = doc.Descendants()
    .Where(e => !e.HasElements && e.Value == "<Text>")
    .FirstOrDefault();

var target = new XElement(source);
target.Value = textLine;
source.AddAfterSelf(target);

doc.Save(...);

Try this

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.Xml.Linq; namespace ConsoleApplication1 { class Program { const string FILENAME = @"c:\\temp\\test.xml"; static void Main(string[] args) { XElement doc = XElement.Load(FILENAME); XElement p = doc.Descendants().Where(x => x.Name.LocalName == "p").FirstOrDefault(); XAttribute name = p.Attributes().Where(x => x.Name.LocalName == "style-name").FirstOrDefault(); name.Value = "new value"; doc.Save(FILENAME); } } } 

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