Csharpz
Csharpz

Reputation: 925

how to save XML fragment?

the code:

    var readerSettings = new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Fragment };
        using (var reader = XmlReader.Create(inputz, readerSettings))
        {
            while (reader.Read())
            {
                using (var fragmentReader = reader.ReadSubtree())
                {
                    if (fragmentReader.Read())
                    {
                        var fragment = XNode.ReadFrom(fragmentReader) as XElement;

what its outputting:

    <sys>
      <id>FSB</id>
      <label>FSB</label>
      <value>266</value>
    </sys>
     <sys>
      <id>CPU</id>
      <label>speed</label>
      <value>2930</value>
    </sys>

how would i change it so that it writes "value" to a string instead? so the output looks like : ,266,2930

thanks

Upvotes: 0

Views: 263

Answers (2)

Kirk Broadhurst
Kirk Broadhurst

Reputation: 28718

Well the obvious answer is to convert to a string -

var stringFragment = fragment.ToString();

or, if necessary,

var fragment = (XNode.ReadFrom(fragmentReader) as XElement).ToString();

I'm not 100% sure this will achieve what you're trying to do, but it will return your XML snippet as a string.

edit

Now that the question has been updated, I can give some more information. I'll assume that each fragment is in fact the sys node.

You first need to get the value node, and then retrieve its 'value' (ie its content).

var valueNode = fragment.Element("value");
var content = valueNode.Value;

Upvotes: 1

Henk Holterman
Henk Holterman

Reputation: 273274

I would guess:

 var fragment = (XNode.ReadFrom(fragmentReader)).ToString();

Upvotes: 1

Related Questions