I'm trying to write something that quickly will grab field values (e.g. combo box, lookups, etc) using the Sharepoint Web Services. The following code works, but is slow and seems inefficient. Is there any way to turn this into a LINQ style query with XDocument/XElement? When I try to Parse the OuterXml it seems to load incorrectly.
MSDN - Lists.GetList
ProuductionResultNode = listservice.GetList(productiontable_listGUID);
XmlDocument doc = new XmlDocument();
doc.LoadXml(ProuductionResultNode.OuterXml);
XmlNamespaceManager mg = new XmlNamespaceManager(doc.NameTable);
mg.AddNamespace("sp", "http://schemas.microsoft.com/sharepoint/soap/");
mg.AddNamespace("z", "#RowsetSchema");
mg.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");
mg.AddNamespace("y", "http://schemas.microsoft.com/sharepoint/soap/ois");
mg.AddNamespace("w", "http://schemas.microsoft.com/WebPart/v2");
mg.AddNamespace("d", "http://schemas.microsoft.com/sharepoint/soap/directory");
XmlNodeList FieldsInList = doc.SelectNodes("//sp:Field", mg);
foreach (XmlNode Field in FieldsInList)
{
if (Field.HasChildNodes)
{
if (Field.Attributes["Name"].Value == fieldNameInternal)
{
foreach (XmlNode node in Field.ChildNodes)
{
if (node.HasChildNodes)
{
foreach (XmlNode Newnode in node.ChildNodes)
{
if (Newnode.HasChildNodes)
{
ret.Add(Newnode.InnerText);
}
}
}
}
}
}
}
return ret;
An example dropdown Field looks like this:
<Field Type="Choice" DisplayName="Media Type" Required="FALSE" Format="Dropdown" FillInChoice="FALSE" ID="{d814daf1-0bd2-48cc-8709-a513a3de4ef4}" SourceID="{1c01c034-f1fd-447f-8ed4-d60b997d0c3a}" StaticName="Media_x0020_Type" Name="Media_x0020_Type" ColName="nvarchar4" RowOrdinal="0" Version="3"><Default>CD/DVD</Default><CHOICES><CHOICE>CD/DVD</CHOICE><CHOICE>Hard Drive</CHOICE><CHOICE>Flash Drive</CHOICE><CHOICE>Virtual</CHOICE></CHOICES></Field>
The other queries I am using for GetListItems seems to work beautfiully.
XElement ziprecords = XElement.Parse(ZipItemsResultNode.OuterXml);
XName name = XName.Get("data", "urn:schemas-microsoft-com:rowset");
var iterationNotes =
from ele in ziprecords.Element(name).Elements()
where ele.Attribute("ows_Title").Value.Contains(fileName
where ele.Attribute("ows_Iteration_x0020_Notes") != null
select new { iterationNote = ele.Attribute("ows_Iteration_x0020_Notes").Value,
fileName = ele.Attribute("ows_Title").Value };
My Solution
I came up with a solution after some searching. I'm not sure why regular Parse and other methods didn't work, but this seems to fix whatever mistakes I was making. I"ll leave this here incase somebody can explain why these steps are needed. I suspect there was an issue with the encoding, which I assumed XDcoument/etc would naturally take care of.
ProuductionResultNode = listservice.GetList(productiontable_listGUID);
XmlDocument xdoc = new XmlDocument();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdoc.NameTable);
nsmgr.AddNamespace("ans", "http://schemas.microsoft.com/sharepoint/soap/");
byte[] byteArray = Encoding.ASCII.GetBytes(ProuductionResultNode.SelectSingleNode(".//ans:Fields", nsmgr).OuterXml);
MemoryStream stream = new MemoryStream(byteArray);
XElement xe = XElement.Load(stream);
XElement qry =
(from field in xe.Descendants()
where field.Attribute("Name") != null
where field.Attribute("Name").Value == "Ship_x0020_Via"
select field).Single();
List<string> ret = new List<string>();
foreach (XElement xle in qry.XPathSelectElements(".//ans:CHOICES", nsmgr).Elements())
{
ret.Add(xle.Value);
}
Related
I am working on this since yesterday. I have an XML file which looks something like this
<catalog>
<captureInfo>
<row>5</row>
<col>5</col>
</captureInfo>
<patientInfo>
<name>XYZ</name>
<detail>details here</detail>
</patientInfo>
<imageData>
<r0c0>
<contrastFlag>true</contrastFlag>
</r0c0>
<r0c1>
<contrastFlag>true</contrastFlag>
</r0c1>
</imageData>
</catalog>
I need to update the value of contrastFlag in the XML file. This is the code I have written:
XmlDocument doc = new XmlDocument();
XmlNodeList imageData = doc.GetElementsByTagName("imageData");
foreach(XmlNode node in imageData)
{
foreach (XmlNode innernode in node)
{
if (innernode.Name == "r0c0")
{
innernode.InnerText = "false";
}
}
}
doc.Save("XMLFile1.xml");
Can anyone tell me where am I going wrong and also is there any better/faster approach for this?
Well first off, your XML is malformed, the closing should match "catalog". Why not just do this:
string xml = #"<catalog>
<captureInfo>
<row>5</row>
<col>5</col>
</captureInfo>
<patientInfo>
<name>XYZ</name>
<detail>details here</detail>
</patientInfo>
<imageData>
<r0c0>
<contrastFlag>true</contrastFlag>
</r0c0>
<r0c1>
<contrastFlag>true</contrastFlag>
</r0c1>
</imageData>
</catalog>";
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(xml);
xdoc.SelectSingleNode("//catalog/imageData/r0c0/contrastFlag").InnerText = "false";
Here is a way to replace all of the instances using LINQ. I just wrote out to a new file to preserve the source.
StreamReader stream = new StreamReader(#"c:\test.xml");
XDocument doc = XDocument.Load(stream);
IEnumerable<XElement> flags = doc.Descendants("contrastFlag");
foreach (XElement e in flags)
{
e.Value = "false";
}
doc.Save(#"c:\test2.xml");
I am trying to read an XML feed from http://itunes.apple.com/us/rss/topsongs/limit=10/genre=2/xml.
I want to access the fields like this:
<im:price amount="1.29000" currency="USD">$1.29</im:price>
<im:releaseDate label="December 31, 1960">1960-12-31T16:00:00-07:00</im:releaseDate>
Here is what I have done so far:
var xml = "http://itunes.apple.com/us/rss/topsongs/limit=10/genre=2/xml";
XmlDocument doc = new XmlDocument();
doc.Load(xml);
XmlNodeList items = doc.SelectNodes("//entry");
foreach (var item in items) {
// Do something with item.
}
No luck, though. items is null. Why? What am I doing wrong?
You need to create a namespace manager to map the RSS and also the iTunes custom tags namespace URIs to short prefixes (itunes and im in the example below):
var xml = "http://itunes.apple.com/us/rss/topsongs/limit=10/genre=2/xml";
XmlDocument doc = new XmlDocument();
doc.Load(xml);
var namespaceManager = new XmlNamespaceManager(doc.NameTable);
namespaceManager.AddNamespace("itunes", "http://www.w3.org/2005/Atom");
namespaceManager.AddNamespace("im", "http://itunes.apple.com/rss");
XmlNodeList items = doc.SelectNodes("//itunes:entry", namespaceManager);
foreach (XmlNode item in items)
{
var price = item.SelectSingleNode("im:price", namespaceManager);
var releaseDate = item.SelectSingleNode("im:releaseDate", namespaceManager);
if (price != null)
{
Console.WriteLine(price.Attributes["amount"].InnerText);
}
if (releaseDate != null)
{
Console.WriteLine(releaseDate.Attributes["label"].InnerText);
}
}
For that specific feed you should get 10 entries.
It's in the docs as well:
If the XPath expression does not include a prefix, it is assumed that
the namespace URI is the empty namespace. If your XML includes a
default namespace, you must still use the XmlNamespaceManager and add
a prefix and namespace URI to it; otherwise, you will not get any
nodes selected. For more information, see Select Nodes Using XPath
Navigation.
Alternatively you can use a namespace-agnostic XPath (from here):
XmlNodeList items = doc.SelectNodes("//*[local-name() = 'entry']");
Finally, not sure why you said items is null. It cannot be. When running your original code you should get this:
I was getting values from an XML file and placing them inside a dataGridView. I was successful in doing so, but after I want to manipulate data that I got from an XML file, It does not work and i get an error of Input string was not in a correct format. .
My goal is to convert a data captured from an XML File and divide it by 1024. Ain't InnerText a string that I can safely convert to a long? should I add more code to make this work?
During my debugging, i printed out the value of temp and the Value is 53999759360, I also tried not making it ToString() , same error
Here is part of my code: (The value of size is "53999759360")
XmlDocument doc = new XmlDocument();
string xmlFilePath = #"C:\xampp\htdocs\userInfo.xml";
doc.Load(xmlFilePath);
XmlNodeList accountList = doc.GetElementsByTagName("account");
foreach (XmlNode node in accountList)
{
XmlElement accountElement = (XmlElement)node;
foreach (XmlElement dskInterface in node.SelectNodes("systemInfo/dskInfo/dskInterface"))
{
String temp = (dskInterface["size"].InnerText).ToString();
long iasdas = Convert.ToInt64(temp) / 1024; // Error Happens here
}
}
I'm afraid that your code works fine. It must be that the "temp" variable is string.Empty or whitespace.
I created an XmlDocument (from XDocument, sorry. I think it's a lot easier to work with) that looks like what you're targeting and ran your code. It runs fine and gives a proper value:
var xDoc = new XDocument(
new XDeclaration("1.0", "UTF-8", "no"),
new XElement("root",
new XElement("account",
new XElement("systemInfo",
new XElement("dskInfo",
new XElement("dskInterface",
new XElement("size", 53999759360)))))));
var doc = new XmlDocument();
using (var xmlReader = xDoc.CreateReader())
{
doc.Load(xmlReader);
}
XmlNodeList accountList = doc.GetElementsByTagName("account");
foreach (XmlNode node in accountList)
{
XmlElement accountElement = (XmlElement)node;
foreach (XmlElement dskInterface in node.SelectNodes("systemInfo/dskInfo/dskInterface"))
{
String temp = (dskInterface["size"].InnerText).ToString();
long iasdas = Convert.ToInt64(temp) / 1024; // Error Happens here
}
}
Edit:
Here's an easier way to test what's actually happening:
Convert.ToInt64(null); // Doesn't crash
Convert.ToInt64(string.Empty); // Crashes
Convert.ToInt64(""); // Will crash if you comment the line above
Convert.ToInt64(" "); // Will crash if you comment the lines above
I would like to get XmlNodeList from a huge XML file.
Conditions:
I have a List of unique ID values, say IDList
Case I: Collect all the nodes where element called ID has value from IDList.
Case II: Collect all nodes where one of the attribute called idName of element ID has value from IDList.
In short, extract only the nodes which match with the values given in the IDList.
I did this using some loops like load this XML to XmlDocument to iterate over all nodes and ID value but what I am looking for is some sophisticated method to do it faster and in quick way.
Because looping isn't a solution for a large XML file.
My try:
try
{
using (XmlReader reader = XmlReader.Create(URL))
{
XmlDocument doc = new XmlDocument();
doc.Load(reader);
XmlNodeList nodeList = doc.GetElementsByTagName("idgroup");
foreach (XmlNode xn in nodeList)
{
string id = xn.Attributes["id"].Value;
string value = string.Empty;
if (IDList.Contains(id))
{
value = xn.ChildNodes[1].ChildNodes[1].InnerText; // <value>
if (!string.IsNullOrEmpty(value))
{
listValueCollection.Add(value);
}
}
}
}
}
catch
{}
XML (XLIFF) structure:
<XLIFF>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2">
<file date="2013-07-17">
<body>
<id idName="test_001" >
<desc-group name="test_001">
<desc type="text"/>
</desc-group>
<result-unit idName="test_001_text">
<source>abcd</source>
<result>xyz</result>
</result-unit>
</id>
</body>
</file>
</xliff>
Collect all the nodes like above where idName matches.
EDIT
This is a test that can parse the example you are giving. It attempts to reach the result node directly, so that it stays as efficient as possible.
[Test]
public void TestXPathExpression()
{
var idList = new List<string> { "test_001" };
var resultsList = new List<string>();
// Replace with appropriate method to open your URL.
using (var reader = new XmlTextReader(File.OpenRead("fixtures\\XLIFF_sample_01.xlf")))
{
var doc = new XmlDocument();
doc.Load(reader);
var root = doc.DocumentElement;
// This is necessary, since your example is namespaced.
var nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("x", "urn:oasis:names:tc:xliff:document:1.2");
// Go directly to the node from which you want the result to come from.
foreach (var nodes in idList
.Select(id => root.SelectNodes("//x:file/x:body/x:id[#idName='" + id + "']/x:result-unit/x:result", nsmgr))
.Where(nodes => nodes != null && nodes.Count > 0))
resultsList.AddRange(nodes.Cast<XmlNode>().Select(node => node.InnerText));
}
// Print the resulting list.
resultsList.ForEach(Console.WriteLine);
}
You can extract only those nodes you need by using an XPath query. A brief example on how you 'd go about it:
using (XmlReader reader = XmlReader.Create(URL))
{
XmlDocument doc = new XmlDocument();
doc.Load(reader);
foreach(var id in IDList) {
var nodes = doc.SelectNodes("//xliff/file/body/id[#idName='" + id + "']");
foreach(var node in nodes.Where(x => !string.IsNullOrEmpty(x.ChildNodes[1].ChildNodes[1].InnerText)))
listValueCollection.Add(node.ChildNodes[1].ChildNodes[1].InnerText);
}
}
The xpath expression is of course an example. If you want, you can post an example of your XML so I can give you something more accurate.
I'm trying to parse XML returned from the Youtue API. The APIcalls work correctly and creates an XmlDocument. I can get an XmlNodeList of the "entry" tags, but I'm not sure how to get the elements inside such as the , , etc...
XmlDocument xmlDoc = youtubeService.GetSearchResults(search.Term, "published", 1, 50);
XmlNodeList listNodes = xmlDoc.GetElementsByTagName("entry");
foreach (XmlNode node in listNodes)
{
//not sure how to get elements in here
}
The XML document schema is shown here: http://code.google.com/apis/youtube/2.0/developers_guide_protocol_understanding_video_feeds.html
I know that node.Attributes is the wrong call, but am not sure what the correct one is?
By the way, if there is a better way (faster, less memory) to do this by serializing it or using linq, I'd be happy to use that instead.
Thanks for any help!
Here some examples reading the XmlDocument. I don't know whats fastest or what needs less memory - but i would prefer Linq To Xml because of its clearness.
XmlDocument xmlDoc = youtubeService.GetSearchResults(search.Term, "published", 1, 50);
XmlNodeList listNodes = xmlDoc.GetElementsByTagName("entry");
foreach (XmlNode node in listNodes)
{
// get child nodes
foreach (XmlNode childNode in node.ChildNodes)
{
}
// get specific child nodes
XPathNavigator navigator = node.CreateNavigator();
XPathNodeIterator iterator = navigator.Select(/* xpath selector according to the elements/attributes you need */);
while (iterator.MoveNext())
{
// f.e. iterator.Current.GetAttribute(), iterator.Current.Name and iterator.Current.Value available here
}
}
and the linq to xml one:
XmlDocument xmlDoc = youtubeService.GetSearchResults(search.Term, "published", 1, 50);
XDocument xDoc = XDocument.Parse(xmlDoc.OuterXml);
var entries = from entry in xDoc.Descendants("entry")
select new
{
Id = entry.Element("id").Value,
Categories = entry.Elements("category").Select(c => c.Value)
};
foreach (var entry in entries)
{
// entry.Id and entry.Categories available here
}
I realise this has been answered and LINQ to XML is what I'd go with but another option would be XPathNavigator. Something like
XPathNavigator xmlNav = xmlDoc.CreateNavigator();
XPathNodeIterator xmlitr = xmlNav.Select("/XPath/expression/here")
while (xmlItr.MoveNext()) ...
The code is off the top of my head so it may be wrong and there may be a better way with XPathNavigator but it should give you the general idea
You could use XSD.exe to generate a class based on the schema provided. Once generated, you could then parse the XML response into the strongly typed class.
string xmlResponse = GetMyYouTubeStuff();
MyYouTubeClass response = null;
XmlHelper<MyYouTubeClass> xmlHelper = new XmlHelper<MyYouTubeClass>();
response = xmlHelper.Deserialize(xmlResponse);
And the class for deserializing it...
public class XmlHelper<T>
{
public T Deserialize(string xml)
{
XmlSerializer xs = new XmlSerializer(typeof(T));
Byte[] byteArray = new UTF8Encoding().GetBytes(xml);
MemoryStream memoryStream = new MemoryStream(byteArray);
XmlTextReader xmlTextReader = new XmlTextReader(memoryStream);
T retObj = (T)xs.Deserialize(xmlTextReader);
return retObj;
}
}
There's also another way here.