2

I have a xml that may contains several types of nodes, but I am only interested in some specific node types. I want to change the nodes that I am interested in and save the result, when other nodes should be unchanged.

for example my xml is as follow:

<?xml version="1.0" encoding="UTF-8"?>
    <Unknown>
     ... 
    <UnKnown/>

    <CompanyName>
        <AttrContainer>
            <Attr type="String">
                <Name value="'Name'" />
                <Value value="'AttrContainer'" />
            </Attr>
            <Unknown>
              ... 
            <UnKnown/>

            <SubContainer>
                <AttrContainer value="'WSSMetadata'" />
                <AttrContainer>
                    <Attr type="String">
                        <Name value="'Name'" />
                        <Value value="'AttrContainer'" />
                    </Attr>
                    <SubContainer>
                        <WSSMetadata value="'afe2e194-0ce7-4bfc-b446-9623e4fe7189'" />
                        <AttrContainer>
                            <Attr type="String">
                                <Name value="'Name'" />
                                <Value value="'WSSMetadata'" />
                            </Attr>
                            <Attr type="Uuid">
                                <Name value="'scanID'" />
                                <Value value="afe2e194-0ce7-4bfc-b446-9623e4fe7189" />
                            </Attr>
                            <Attr type="String">
                                <Name value="'imagePath'" />
                            </Attr>
                            <Attr type="String">
                                <Name value="'imagePathHD'" />
                            </Attr>
                            <Attr type="String">
                                <Name value="'imagePathThumbnail'" />
                            </Attr>
                            <Attr type="String">
                                <Name value="'imagePathGrey'" />
                                <Value value="'Images/afe2e194-0ce7-4bfc-b446-9623e4fe7189_grey.jpg'" />
                            </Attr>
                            <Attr type="String">
                                <Name value="'imagePathGreyHD'" />
                                <Value value="'Images/afe2e194-0ce7-4bfc-b446-9623e4fe7189_grey_hd.jpg'" />
                            </Attr>
                            <Attr type="String">
                                <Name value="'imagePathGreyThumbnail'" />
                                <Value value="'Images/afe2e194-0ce7-4bfc-b446-9623e4fe7189_grey_thumbnail.jpg'" />
                            </Attr>
                        </AttrContainer>
                    </SubContainer>
                </AttrContainer>
            </SubContainer>
        </AttrContainer>
    </CompanyName>

I want to change for example 'Images/afe2e194-0ce7-4bfc-b446-9623e4fe7189_grey_thumbnail.jpg' to something else, and write back the XML, the main point is that I don't want to delete or change <unknown> sections.

I wrote code to read the nodes that I am interested in and the code can read them and extract data from them, but how do I write the reset when I have no information about the other nodes?

aevitas
  • 3,753
  • 2
  • 28
  • 39
mans
  • 17,104
  • 45
  • 172
  • 321

3 Answers3

1

The xslt identity transform is a good starting point for this kind of work, i.e.:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

Then, add a template for the element(s) of the xml that you DO want to modify.

StuartLC
  • 104,537
  • 17
  • 209
  • 285
  • Thanks. Is there any sample? or tutorial? – mans Mar 24 '14 at 10:44
  • Example of [using an xsl transform in C#](http://stackoverflow.com/questions/34093/how-to-apply-an-xslt-stylesheet-in-c-sharp): . Example of using the identity transform with [another custom template](http://stackoverflow.com/a/8009841/314291) – StuartLC Mar 24 '14 at 10:46
0

If it is necessary to do in in code, you can read the File as follows.

String FileContent = null;
using (StreamReader streamReader = new StreamReader(@"TheFile.xml"))
{
    FileContent = streamReader.ReadToEnd();
}
XmlDocument Doc = new XmlDocument();
Doc.LoadXml(FileContent);

After doing the desired modifications in code, it can be saved back to the file.

Codor
  • 17,447
  • 9
  • 29
  • 56
  • How can I make modification? For example if I want to have . Please note this item doesn't have any value node. – mans Mar 24 '14 at 11:04
  • You can select the document's root via `XmlNode Root = (XmlNode)Doc.SelectSingleNode("descendant::report");` and acces `Root`, its children, attributes and the like and manipulate them. – Codor Mar 24 '14 at 11:22
0

As an alternative to XSLT transformation (which is a good fit for a task), you can just save an XML document.

A full approach:

  • Read a document into memory
  • Locate the element you need to change (LINQ-to-XML, xpath, something else ...)
  • Change the element in a document
  • Ensure the document is closed at this stage, otherwise give your document a new name to allow saving
  • Save full document (for example using XmlDocument.Save)
oleksii
  • 35,458
  • 16
  • 93
  • 163