0

I have a XML configuration for Query CAML:

<add key="QueryList" value="&lt;Query&gt;&lt;Where&gt;&lt;Eq&gt;&lt;FieldRef Name='Cargar_x0020_Optimyth'/&gt;&lt;Value Type='Boolean'&gt;1&lt;/Value&gt;&lt;/Eq&gt;&lt;/Where&gt;&lt;/Query&gt;" />
<add key="PaginacionList" value="10" />
<add key="QueryOptions" value="&lt;IncludeMandatoryColumns&gt;FALSE&lt;/IncludeMandatoryColumns&gt;&lt;Paging ListItemCollectionPositionNext=''/&gt;" />

Now, I want do this:

    XElement ndQuery = XElement.Parse(Configuracion.QueryList);
    XElement ndViewFields = XElement.Parse(Configuracion.ViewFields);
    XElement ndQueryOptions = XElement.Parse(Configuracion.QueryOptions);

but I get an error.

I try this using XmlElement and its working:

    XmlElement ndQuery = xmlDoc.CreateElement("Query");
    if (!String.IsNullOrEmpty(Configuracion.QueryList))
    {
        ndQuery.InnerXml = Configuracion.QueryList;
    }
    XmlElement ndViewFields = xmlDoc.CreateElement("ViewFields");
    if (!String.IsNullOrEmpty(Configuracion.ViewFields))
    {
        ndViewFields.InnerXml = Configuracion.ViewFields;
    }
    XmlElement ndQueryOptions = xmlDoc.CreateElement("QueryOptions");
    if (!String.IsNullOrEmpty(Configuracion.QueryOptions))
    {
        ndQueryOptions.InnerXml = Configuracion.QueryOptions;
    }

    XElement ndQuery = XElement.Parse(ndQuery2.OuterXml);
    XElement ndViewFields = XElement.Parse(ndViewFields2.OuterXml);
    XElement ndQueryOptions = XElement.Parse(ndQueryOptions2.OuterXml);

I want avoid use XmlElement, and only XElement.

Any solution about it?

Kiquenet
  • 14,494
  • 35
  • 148
  • 243

2 Answers2

0

Your XML in the QueryOptions setting isn't an element. It's two sibling elements. You need one surrounding them, which in this case would be <QueryOptions>:

<add key="QueryOptions" 
 value="&lt;QueryOptions&gt;&lt;IncludeMandatoryColumns&gt;FALSE&lt;/IncludeMandatoryColumns&gt;&lt;Paging ListItemCollectionPositionNext=''/&gt;&lt;/QueryOptions&gt;" />
JLRishe
  • 99,490
  • 19
  • 131
  • 169
-1

Quite complex because you have an Xml fragment (multiple root nodes) in Configuracion.QueryOptions. The XLinq doesn't have direct methods to handle it... Based on Fragmented XML string parsing with Linq, you could

public static IEnumerable<XNode> ParseXml(string xml)
{
    // Note the added escaping
    // You can replace it with WebUtility.HtmlDecode
    // They are defined in different assemblies
    xml = HttpUtility.HtmlDecode(xml);

    var settings = new XmlReaderSettings
    {
        ConformanceLevel = ConformanceLevel.Fragment,
        IgnoreWhitespace = true
    };

    using (var stringReader = new StringReader(xml))
    using (var xmlReader = XmlReader.Create(stringReader, settings))
    {
        xmlReader.MoveToContent();
        while (xmlReader.ReadState != ReadState.EndOfFile)
        {
            yield return XNode.ReadFrom(xmlReader);
        }
    }
}

and then

XElement ndQuery = new XElement("Query", ParseXml(Configuracion.QueryList));
XElement ndViewFields = new XElement("ViewFields", ParseXml(Configuracion.ViewFields));
XElement ndQueryOptions = new XElement("QueryOptions", ParseXml(Configuracion.QueryOptions));

The use of HttpUtility.HtmlDecode/WebUtility.HtmlDecode is from Built in .NET function for unescaping characters in XML stream? .

Or perhaps you want to keep the configurations encoded as they are... then

XElement ndQuery = new XElement("Query", HttpUtility.HtmlDecode(Configuracion.QueryList));
XElement ndViewFields = new XElement("ViewFields", HttpUtility.HtmlDecode(Configuracion.ViewFields));
XElement ndQueryOptions = new XElement("QueryOptions", HttpUtility.HtmlDecode(Configuracion.QueryOptions));

(much shorter :-) )

Community
  • 1
  • 1
xanatos
  • 109,618
  • 12
  • 197
  • 280