Getting RSS item <description> with XmlNodeList - c#

I'm trying to get the description from a podcast item in an rss feed but I want it to start reading after any extra tags after the description such as a paragraph tag.
XmlNodeList xmlNodeListDesc = myXmlDocument.GetElementsByTagName("description");
List<Podd> poddLista = new List<Podd>();
for (int i = 2; i < xmlNodeListDur.Count; i++)
{
podd.description = xmlNodeListDesc[i].InnerText;
poddLista.Add(podd);
}
< description>< ![ CDATA [< p >Desired text< / p >]]> < / description>
My current code would start reading after CDATA so < p > and any style tags would be included in the description.

Related

Getting: "Document would result in an invalid XML document" error

I'm trying to split up a xml document into multiple smaller documents. I want to pre define a badge size (max number of nodes / document) and then insert the data into it. There are 2 possible structures of my xml data:
<?xml version='1.0' encoding='UTF-8' ?>
<V2:EndInvoices">
<V2:EndInvoice>
</V2:EndInvoice>
...
</V2:EndInvoices>
<?xml version='1.0' encoding='UTF-8' ?>
<tls:AkontoGroup">
<tls:AkontoMember>
</tls:AkontoMember>
...
</tls:AkontoGroup>
Right now I'm focusing on only one case. Each rechnungen.ToArray()[i] element contains one of these EndInvoice elements. I was able to create 4 files with a input file of 20 invoices split by 5 (batchSize = 5), each file containing one EndInvoice. Then I moved the line batchRechnung.Add(rechnungen.ToArray()[i]); out of the if block, which now causes me the error.
public List<XDocument> createTemporaryXMLFiles(string pathToData, int batchSize)
{
List<XDocument> batchRechnungen = new List<XDocument>();
XDocument batchRechnung = new XDocument();
XElement dataSource = XElement.Load(pathToData);
IEnumerable<XElement> rechnungen = dataSource.Elements();
for(int i = 0; i < rechnungen.ToArray().Length; i++)
{
if (i == 0 || (i % batchSize) == 0)
{
batchRechnung = new XDocument();
batchRechnungen.Add(batchRechnung);
}
batchRechnung.Add(rechnungen.ToArray()[i]);
}
return batchRechnungen;
}
How can I get correct xml files, each containting
<V2:EndInvoices">
batchSize x (<V2:EndInvoice></V2:EndInvoice>)
</V2:EndInvoices>
You cannot add multiple root elements to XDocument. And that's exactly what happens when you write batchRechnung.Add. Therefore, you must add the root element first. And then add elements to it.
public List<XDocument> createTemporaryXMLFiles(string pathToData, int batchSize)
{
List<XDocument> batchRechnungen = new List<XDocument>();
XElement dataSource = XElement.Load(pathToData);
XDocument batchRechnung = new XDocument(new XElement(dataSource.Name));
var rechnungen = dataSource.Elements().ToArray();
for (int i = 0; i < rechnungen.Length; i++)
{
if (i == 0 || (i % batchSize) == 0)
{
batchRechnung = new XDocument(new XElement(dataSource.Name));
batchRechnungen.Add(batchRechnung);
}
batchRechnung.Root.Add(rechnungen[i]);
}
return batchRechnungen;
}

Xml searching recursively for a specific value

In my C# app I am reading an xml document, and in it, are tags containing paths to where .png and .jpg files are being kept. These tags are , and .
I could simply create an XmlNodeList object for each of these tags, such as
XmlNodeList image = _doc.GetElementsByTagName.("Image");
XmlNodeList background = _doc.GetElementsByTagName.("BackgroundImage");
XmlNodeList foreground = _doc.GetElementsByTagName.("ForegroundImage");
for(int i = 0; i < image.count; i++)
{
//..code
}
for(int i = 0; i < background .count; i++)
{
//..code
}
for(int i = 0; i < foreground .count; i++)
{
//..code
}
Clunky, I know. But, is there a way where I can have the application recursively find the tags that contains the word "Image" and return it as a single XmlNodeList? Can it be done? Would this be the best approach? Many thanks in advance.

c# HtmlAgilityPack for on nodes array

I'm using html agility pack and after I got array of nodes:
HtmlNode[] nodes = document.DocumentNode.SelectNodes("//tbody[#class='table']").ToArray();
now i want to run a for loop one each nodes[i]. I've tried this:
for (int i = 0; i < 1; i++)
{
if (t == null)
t = new Model.Track();
HtmlNode[] itemText = nodes[i].SelectNodes("//td[#class='artist']").ToArray();
for (int x = 0; x < itemText.Length; x++)
{ //doing something }
the problem is that the itemtext array isn't focusing on nodes[i] .
but brings out an array of all the ("//td[#class='artist']") in the html document.
help?
Using //td[#class='artist'] will fetch all columns with artist class from your document.DocumentNode.
Using .//td[#class='artist'] (Notice the dot at the begining) will fetch all columns with artist class from the current selected node, which in your case is nodes[i].

Get label of svg element according to its id

I want to get label of svg element. I wrote a console application and thougt svg file as xml. I am trying to get labels according to id of element. When i wrote code below, i got all ids but i cant get label with that row. How can i access the label? An example of element is;
<rect
style="fill:#cccccc"
id="21"
width="35.823246"
height="35.823246"
x="299.87155"
y="65.999405"
class="seatObj"
inkscape:label="A22" />
And codes are;
class Program
{
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.Load(#"seatChart-01.svg");
XmlNodeList elementList = doc.GetElementsByTagName("rect");
string[] labels = new string[elementList.Count];
for (int i = 0; i < elementList.Count; i++)
{
int id = int.Parse(elementList[i].Attributes["id"].Value);
labels[id] = elementList[i].Attributes["inkspace:label"].Value;
}
for (int i = 0; i < labels.Length; i++)
{
Console.WriteLine(labels[i]);
}
Console.ReadLine();
}
}
elementList[i].Attributes["inkspace:label"].Value
should be
elementList[i].Attributes["label", "http://www.inkscape.org/namespaces/inkscape"].Value
I'm assuming that the namespace is that of the inkscape drawing program here. To confirm that look for something on the root element that looks like this...
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"

Why do I need to count the number of XmlNodes before iterating through and deleting some of them?

I believe I have found a weird bug as follow:
I want to delete the first two nodes in an XmlNodeList.
I know that there may be other ways of doing this (there surely are) but it is the reason why one of the code segments works and one doesn't (the difference being the Count line) that I am interested in.
var strXml = #"<food><fruit type=""apple""/><fruit type=""pear""/><fruit type=""banana""/></food>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(strXml);
XmlNodeList nlFruit = doc.SelectNodes("food/fruit");
for(int i = 0; i < 2; i++)
{
// This produces a null reference exception:
nlFruit[i].ParentNode.RemoveChild(nlFruit[i]);
}
However, if I count the number of nodes in the XmlNodeList it works and I am left with the desired outcome:
var strXml = #"<food><fruit type=""apple""/><fruit type=""pear""/><fruit type=""banana""/></food>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(strXml);
XmlNodeList nlFruit = doc.SelectNodes("food/fruit");
// Count the nodes..
Debug.WriteLine(nlFruit.Count);
for(int i = 0; i < 2; i++)
{
nlFruit[i].ParentNode.RemoveChild(nlFruit[i]);
}
// doc is now: <food><fruit type="banana" /></food>
Both are wrong you should delete from the end
for(int i = 1; i >= 0; i--)
{
nlFruit[i].ParentNode.RemoveChild(nlFruit[i]);
}
because you remove the 0 th element, and 1 st element becomes the 0 th, than you removes 1st element which is null.
May be this will help:
Halloween Problem : http://blogs.msdn.com/mikechampion/archive/2006/07/20/672208.aspx

Categories

Resources