I have a XML configuration for Query CAML:
<add key="QueryList" value="<Query><Where><Eq><FieldRef Name='Cargar_x0020_Optimyth'/><Value Type='Boolean'>1</Value></Eq></Where></Query>" />
<add key="PaginacionList" value="10" />
<add key="QueryOptions" value="<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns><Paging ListItemCollectionPositionNext=''/>" />
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?
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="<QueryOptions><IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns><Paging ListItemCollectionPositionNext=''/></QueryOptions>" />
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 :-) )
Related
How do we iterate an XmlDocument using an xpath?
I'm attempting to return a list of nodes by xpath:
public static List<string> Filter(string xpath, string input, string ns, string nsUrl)
{
var bytes = Encoding.UTF8.GetBytes(input); //i believe this unescapes the string
var stream = new MemoryStream(bytes);
var doc = new XmlDocument();
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(doc.NameTable);
namespaceManager.AddNamespace(ns, nsUrl);
var links = new List<string>();
var nodes = doc.SelectNodes(xpath, namespaceManager);
using (var reader = new XmlTextReader(stream))
{
reader.Namespaces = false;
doc.Load(reader);
}
foreach (XmlNode node in nodes)
{
if (IsNullOrWhiteSpace(node.InnerText))
{
continue;
}
links.Add(node.InnerText);
}
return links;
}
however, the count is always 0 !
I'm using this xpath. notice how i am using only 1 namespace:
/ns0:Visit/ns0:DocumentInterface/ns0:Documents/ns0:Document/ns0:BinaryData
The header of the file looks like this:
<ns0:Visit xmlns:ns0="http://NameSpace.ExternalSchemas.Patient"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
I'm certain that I am using the right xpath because I tested it against my payload:
I'm calling the function this way:
var links = Filter(xpath, xml, "ns0", "http://NameSpace.ExternalSchemas.Patient");
How do we iterate an XmlDocument using an xpath? Perhaps the XmlDocument should be an XDocument instead?
I am trying to create a xml document of following format:
<![CDATA[<Caption xmlns="http:happy.x.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.happybus.tv/yy/happybus.xsd">
<TemplateID>xxxxx</TemplateID>
<CaptionOptions>
<CaptionField>
<Field>xxx</Field>
<Text>xxx</Text>
</CaptionField>
<CaptionField>
<Field>xxxx</Field>
<Text>""</Text>
</CaptionField>
</CaptionOptions>
</Caption>]]>
Here is the code that I wrote
XmlDocument xml2 = new XmlDocument();
XmlElement e = xml2.CreateElement("Caption");
e.InnerText ="Hello";
XmlElement template = xml2.CreateElement("TemplateID");
template.InnerText = "#TemplateID";
XmlElement captionOptions = xml2.CreateElement("CaptionOptions");
XmlElement captionField = xml2.CreateElement("CaptionField");
XmlElement fieldId = xml2.CreateElement("FieldID");
fieldId.InnerText = "#FieldID";
XmlElement textstring = xml2.CreateElement("TextString");
textstring.InnerText = "#TextString";
captionField.AppendChild(fieldId);
captionField.AppendChild(textstring);
captionOptions.AppendChild(captionField);
e.AppendChild(template);
e.AppendChild(captionOptions);
xml2.AppendChild(e);
StringWriter string_writer2 = new StringWriter();
XmlTextWriter xml_text_writer2 = new XmlTextWriter(string_writer2);
xml_text_writer2.Formatting = Formatting.Indented;
xml2.WriteTo(xml_text_writer2); // xml is your XmlDocument
string formattedXml2 = string_writer2.ToString();
Console.Write(formattedXml2);
I have tried a similar example with different XML doc but it clearly work, I even tried debugging but it is not getting formatted.
Have you tried using the XDocument and related classes? I find they make manual construction of xml easier and more intuitive since the code looks very similar to the xml. The ToString method seems to output the xml formatted the way you want:
void Main()
{
var xDoc = new XDocument
(
new XElement("Parent",
new XElement("TemplateID", "xxxxx"),
new XElement("CaptionOptions",
new XElement("CaptionField",
new XElement("Field", "xxx"),
new XElement("Text", "xxx")
),
new XElement("CaptionField",
new XElement("Field", "xxxx"),
new XElement("Text", "")
)
)
)
);
Console.WriteLine(xDoc.ToString());
//To enclose the xml in a CDATA, you could use:
var cData = new XCData(xDoc.ToString());
Console.WriteLine(cData.ToString());
}
I've spent so much time with this already, still can't get the value of NTE attribute. Can someone please help?
C#
StreamReader sr = new StreamReader(resp.GetResponseStream());
XPathDocument xmlDoc = new XPathDocument(sr); // holds xml document
XPathNavigator xmlNav = xmlDoc.CreateNavigator(); //evaluates XPath expressions
XPathNodeIterator node = xmlNav.Select("/DATA2SC/CALL");
string dne = xmlNav.GetAttribute("NTE", "");
Console.WriteLine(dne);
sr.Close();
XML
<?xml version="1.0"?>
<DATA2SC PIN="00000">
<CALL
TR_NUM="00000001"
STATUS="WAITING_FOR_APPROVAL"
NTE="$15.00">
<PROBLEM>
Text
</PROBLEM>
</CALL>
</DATA2SC>
Can you try this code please ?
I already check and it's work
StreamReader sr = new StreamReader("c:\\x.xml");
XPathDocument xmlDoc = new XPathDocument(sr); // holds xml document
XPathNavigator xmlNav = xmlDoc.CreateNavigator(); //evaluates XPath expressions
var node = xmlNav.SelectSingleNode("/DATA2SC/CALL");
string dne = node.GetAttribute("NTE", "");
Console.WriteLine(dne);
OR
XDocument docXmlWorld = XDocument.Load("c:\\x.xml");
foreach (var node1 in docXmlWorld.Descendants("DATA2SC"))
{
foreach (var node2 in node1.Descendants("CALL"))
{
string dne = node2.Attribute("NTE").Value;
Console.Out.WriteLine(dne);
}
}
Or you can do like this too:
XDocument docXmlWorld = XDocument.Load("c:\\x.xml");
//Get the first child => [DATA2SC]
XElement elementNodeDATA2SC = docXmlWorld.Element("DATA2SC");
//Get the first child => [CALL]
XElement elementNodeCALL = elementNodeDATA2SC.Element("CALL");
//Get the attribute NTE from [CALL] node
string dne = elementNodeCALL.Attribute("NTE").Value;
Console.Out.WriteLine(dne);
The Select method, returns a collection of all nodes with the specified XPath.
You can use SelectSingleNode, to select the first node.
var node = xmlNav.SelectSingleNode("/DATA2SC/CALL");
string dne = node.GetAttribute("NTE", "");
I have got some XML files that contain comments above the nodes. When I am reading the file in, as part of the process I would like to get the comment out as well. I know you can write a comment to the file using XmlComment, but not sure how to read them back out.
My XML looks similar to this:
<Objects>
<!--Comment about node-->
<GUID-bf2401c0-ef5e-4d20-9d20-a2451a199362>
<info job="SAVE" person="Joe" />
<info job="SAVE" person="Sally" />
</GUID-bf2401c0-ef5e-4d20-9d20-a2451a199362>
<!--Another Comment about node-->
<GUID-bf2401c0-ef5e-4d20-9d20-a5844113284112>
<info job="SAVE" person="John" />
<info job="SAVE" person="Julie" />
</GUID-bf2401c0-ef5e-4d20-9d20-a5844113284112>
Try this:
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.IgnoreComments = false;
using (XmlReader reader = XmlReader.Create("input.xml", readerSettings))
{
XmlDocument myData = new XmlDocument();
myData.Load(reader);
// etc...
}
To read comments:
XmlReader xmlRdr = XmlReader.Create("Test.XML");
// Parse the file
while (xmlRdr.Read())
{
switch (xmlRdr.NodeType)
{
case XmlNodeType.Element:
// You may need to capture the last element to provide a context
// for any comments you come across... so copy xmlRdr.Name, etc.
break;
case XmlNodeType.Comment:
// Do something with xmlRdr.value
Using System.Xml.Linq:
var doc = XElement.Load(fileName);
var comments = doc.DescendantNodes().OfType<XComment>();
foreach (XComment comment in comments)
...
They are a part of the child nodes of the containing node as all other nodes: http://msdn.microsoft.com/en-us/library/system.xml.xmlcomment.aspx
I know the question is very old, but yesterday I had the same problem. So here is my solution:
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = false;
settings.IgnoreComments = false;
XmlReaderSettings settings2 = new XmlReaderSettings();
settings2.IgnoreWhitespace = false;
settings2.IgnoreComments = false;
XmlReader xmlreaderOriginalCfg = XmlReader.Create(#"C:\...xml", settings);
XmlReader xmlreaderVerificationCfg = XmlReader.Create(#"C:\....xml", settings);
XmlDocument myData = new XmlDocument();
myData.Load(xmlreaderOriginalCfg);
XmlDocument myData2 = new XmlDocument();
myData2.Load(xmlreaderVerificationCfg);
XmlNode parentNode = myData.SelectSingleNode("/configuration/appSettings");
foreach (XmlComment comment in myData2.SelectNodes("//comment()"))
{
XmlComment importedCom = myData.CreateComment(comment.Value);
parentNode.AppendChild(importedCom);
foreach (XmlNode node in myData2.DocumentElement.SelectNodes("/configuration/appSettings/add"))
{
XmlNode imported = myData.ImportNode(node, true);
parentNode.AppendChild(imported);
}
}
myData.Save(this.pathNew);
Maybe it helps somebody
I stored your XML into a file, here is the code sample.
XmlDocument document = new XmlDocument();
document.Load("test.xml");
foreach (XmlComment comment in document.SelectNodes("//comment()"))
{
Console.WriteLine("Comment: \"{0}\".", comment.Value);
}
Some sample code on how to access comments hope this helps
using System;
using System.IO;
using System.Xml;
public class Sample {
public static void Main() {
XmlDocument doc = new XmlDocument();
doc.LoadXml(#"<Objects><!--Comment about node--><othernode/><!--Some more comment--></Objects>");
XmlNode root = doc.FirstChild;
if (root.HasChildNodes)
{
for (int i=0; i<root.ChildNodes.Count; i++)
{
if( root.ChildNodes[i] is XmlComment)
Console.WriteLine(root.ChildNodes[i].InnerText);
}
}
}
}
i've an xml file like
<Root>
<Steps>
<Step Test="SampleTestOne" Status="Fail" />
<Step Test="SampleTestTwo" Status="Fail" />
</Steps>
</Root>
i need to change or overwrite the attribute value of "Status" in the Step element.
Now i'm using XmlDocument for this
like
XmlDocument XDoc = new XmlDocument();
XDoc.Load(Application.StartupPath + "\\Sample.xml");
XmlNodeList NodeList = XDoc.SelectNodes("//Steps/Step");
foreach (XmlNode Node in NodeList)
{
XmlElement Elem = (XmlElement)Node;
String sTemp = Elem.GetAttribute("Test");
if (sTemp == "SampleTestOne")
Elem.SetAttribute("Status", "Pass");
}
I need search the element and to update the status
is there any way to do this using XDocumentin c#
Thanks in advance
string filename = #"C:\Temp\demo.xml";
XDocument document = XDocument.Load(filename);
var stepOnes = document.Descendants("Step").Where(e => e.Attribute("Test").Value == "SampleTestOne");
foreach (XElement element in stepOnes)
{
if (element.Attribute("Status") != null)
element.Attribute("Status").Value = "Pass";
else
element.Add(new XAttribute("Status", "Pass"));
}
document.Save(filename);
You can use this code:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlFile);
XmlNode node = xmlDoc.SelectSingleNode("Root/Steps/Step");
node.Attributes["Status"].Value = "True";
xmlDoc.Save(xmlFile);