简体   繁体   中英

How to get indented InnerText from an XmlDocument?

Is there a method to get indented text from an XmlDocument in C#? I mean, if there is a build-in method for this. (If there's not a more simpler solution, I'm thinking at one to recursively parse the XML nodes and extract the inner text of each node, while keeping the level of recursiveness as an indentation (with custom character)).

eg

<Element>
  <Children>
    <Element>
      <Messages>
        <Message type="0">
          <Text>[MutationEngine:CraSyncMutationEngine]</Text>
        </Message>
      </Messages>
      <Children>
        <Element>
          <Messages>
            <Message type="0">
              <Text>[Stage:'StageLoad']</Text>
            </Message>
            <Message type="0">
              <Text>[Stage:'StageMergeWithPi']</Text>
            </Message>
            <Message type="0">
              <Text>[Stage:'StageSaveRelationImage']</Text>
            </Message>
          </Messages>
          <Children>
            <Element>
              <Messages>
                <Message type="0">
                  <Text>[PreValidate]</Text>
                </Message>
              </Messages>
            </Element>
            <Element>
              <Messages>
                <Message type="0">
                  <Text>[Action:'LoadPersistedInput']</Text>
                </Message>
              </Messages>
            </Element>
          </Children>
        </Element>
      </Children>
    </Element>
  </Children>
</Element>

should become:

-----[MutationEngine:CraSyncMutationEngine]
-------[Stage:'StageLoad']
-------[Stage:'StageMergeWithPi']
-------[Stage:'StageSaveRelationImage']
---------[PreValidate]
---------[Action:'LoadPersistedInput']

assuming that we are using the - character for indentation. The C# code:

var xmlDoc = new XmlDocument { PreserveWhitespace = true };
xmlDoc.LoadXml(log);

var logInnerText = xmlDoc.InnerText;

Any help is greatly appreciated!

This is one possible way :

var doc = new XmlDocument();
.....
//select all elements having non empty inner text
var nodesHavingInnerText = doc.DocumentElement
                              .SelectNodes("//*[normalize-space(text())]");
var result = "";
foreach (XmlNode node in nodesHavingInnerText)
{
            //put "-" repeated as many as current node's level in the XML doc
    result += string.Concat(Enumerable.Repeat("-", GetLevel(node))) 
                    + 
              node.InnerText 
                    + 
              Environment.NewLine;
}

GetLevel() function is defined as follow (adapted from @Slak's answer to another question : Return node level of hierarchical xml ) :

public int GetLevel(XmlNode node)
{
    if (node.ParentNode == null) return 0;
    return 1 + GetLevel(node.ParentNode);
}

[ .NET fiddle demo ]

I don't undersand exactly what you intend to do, but I hope this will help

string result=xmlDoc.Element("Element")
                    .Element("Children")
                    .Element("Element")
                    .Element("Messages")
                    .Element(Message)
                    .Element("Text").Value;

If you want to get all children of a node use Elements instead of Element and you will get a list of XElement type.

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