I was trying to write an automatic method that retrieve a value from an XML file.
as i wrote the following code:
XmlDocument xDoc = new XmlDocument();
I found out that when I'm trying to access xDoc object while typing xDoc. , it does nothing, means no option to manipulate the object...
my usings are:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using Microsoft.Office.Interop;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;
using System.Xml;
using System.Collections.Generic;
Any ideas ?
Sounds like you need to make a class to put the XMLDocument in if (per your comment) that is the next line you could have attached.
What I mean is, as Jon Skeet said, are you sure Adam that you're not declaring the XMLDocument inside of the namespace instead of inside of a class. Should be something like:
using System.Xml;
namespace SomeNameSpace
{
public class MyClass
{
XMLDocument xDoc = new XMLDocument();
public void MyMethod() {
xDoc.DocumentType;
}
}
}
Related
I am new to managing XML, I've read a couple of articles but I'm confused when it comes to a specific XML that I am working on. Can someone help me with the right statement? I just want to update the value of the ListStart, but I am getting an error when compiling. I am updating that part by this:
XmlDocument soapEnvelopeDocument = new XmlDocument();
soapEnvelopeDocument.Load(#"path");
XmlNode myNode = soapEnvelopeDocument.SelectSingleNode("descendant::cet:GetListCustomElement[cet:GetListCustom/cet:ListID='101']");
soapEnvelopeDocument.LastChild.InnerText = sDate;
<soapenv:Header/>
<soapenv:Body>
<cet:GetListCustomElement>
<!--Zero or more repetitions:-->
<cet:GetListCustom>
<cet:ListID>101</cet:ListID>
<cet:ListStart>13.11.2020</cet:ListStart>
</cet:GetListCustom>
</cet:GetListCustomElement>
</soapenv:Body>
</soapenv:Envelope>```
You only supplied a piece of the xml without the namespaces. With Xml linq you can get the element without the namespaces. See code below :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement ListStart = doc.Descendants().Where(x => x.Name.LocalName == "ListStart").FirstOrDefault();
ListStart.SetValue("14.11.2020");
}
}
}
Basically what I want to do is: Download a new XML file and replace some elements with the old one, For example replace this code:
<Run x:Name="Degree" Text="15"/>
with current degree, which is
<temperature value="280.15" min="278.15" max="281.15" unit="kelvin"/>
but I don't know how to do that. Here's my code I'm stuck with:
using (WebClient web = new WebClient())
{
string url = string.Format("https://samples.openweathermap.org/data/2.5/weather?q=London&mode=xml&appid=b6907d289e10d714a6e88b30761fae22");
var xml = web.DownloadString(url);
}
Use xml linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string URL = "https://samples.openweathermap.org/data/2.5/weather?q=London&mode=xml&appid=b6907d289e10d714a6e88b30761fae22";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(URL);
XElement temperature = doc.Descendants("temperature").FirstOrDefault();
temperature.SetAttributeValue("value", 281);
string oldXml = "<Root xmlns:x=\"abc\"><Run x:Name=\"Degree\" Text=\"15\"/></Root>";
XDocument oldDoc = XDocument.Parse(oldXml);
XElement run = oldDoc.Descendants("Run").FirstOrDefault();
run.ReplaceWith(temperature);
}
}
}
I am using AngleSharp and would like to know how to transform sections of HTML to something like SVG, XML or another custom bracket supported format.
Question: how do I build a custom AngleSharp IElement and then transform the div (or link) contents inside the custom element i.e. take the div/links I found, and then put the div content inside in the custom element
Below is the code I am trying
var divToTransform = document.QuerySelector("div.class1.class2");
// need a custom transform, something simple like replacing the tags
new IElement myCustomeAngleBracketQml = new CustomeAngleBracketQml(divToTransform);
//something simple like replacing the tags
myCustomeAngleBracketQml.Replace("div", Tag).With("QmlDiv");
//insert the content
myCustomeAngleBracketQml.TextContent = divToTransform.innerContent();
//1) how to put this back in the place of the original div, after deleting that div?
///2) *how to target a specific node in the Dom
document./*after Div with class, or Div with Id*/.AppendChild(element);
thanks
use Xml Linq XElement
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
XElement doc = new XElement("QmlDiv", new object[] {
new XElement("class1", new object[] {
new XElement("class2",divToTransform.innerContent)
}),
});
}
}
}
I am trying to extract the XML Response from an API. My XML response looks like below. I tried different methods. can you please help me in reading and extracting the individual nodes from the XMl response.
For Example: I want to extract node from below XML response.
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
<RichDBDS><TrxDetailCard><TRX_HD_Key>5437845</TRX_HD_Key> <Invoice_ID>189809</Invoice_ID><Date_DT>3/24/2016 1:34:44 PM</Date_DT><Merchant_Key>2202</Merchant_Key><Reseller_Key>2</Reseller_Key><TUser_Name_VC>ITSTEST.ISMI</TUser_Name_VC><Processor_ID>CC Processor</Processor_ID><TRX_Settle_Key></TRX_Settle_Key><Tip_Amt_MN></Tip_Amt_MN><Approval_Code_CH>24032016013444</Approval_Code_CH><Auth_Amt_MN>184.99</Auth_Amt_MN><IP_VC>66.55.53.68</IP_VC><Account_Type_CH>MANUAL</Account_Type_CH><Last_Update_DT></Last_Update_DT><Orig_TRX_HD_Key></Orig_TRX_HD_Key><Settle_Date_DT></Settle_Date_DT><Settle_Flag_CH>FALSE</Settle_Flag_CH><Trans_Type_ID>Sale</Trans_Type_ID><Void_Flag_CH>FALSE</Void_Flag_CH><CustomerID></CustomerID><AVS_Resp_CH>Y</AVS_Resp_CH><CV_Resp_CH>M</CV_Resp_CH><Host_Ref_Num_CH>0b144718-3597-4cfb-a2b5-058679510f29</Host_Ref_Num_CH><Zip_CH>30328</Zip_CH><Acct_Num_CH>1111</Acct_Num_CH><Total_Amt_MN>184.99</Total_Amt_MN><Exp_CH>0917</Exp_CH><Name_on_Card_VC>Kenneth Boler</Name_on_Card_VC><Type_CH>VISA</Type_CH><Cash_Back_Amt_MN></Cash_Back_Amt_MN><Result_CH>0</Result_CH><Result_Txt_VC>approval</Result_Txt_VC><Trans_Status>Settled</Trans_Status><PO_Num></PO_Num></TrxDetailCard></RichDBDS>
Try code like below using XML Linq. I used Load(FILENAME or URL) method, but you can also use Parse(STRING).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
var results = doc.Descendants().Where(x => x.Name.LocalName == "TrxDetailCard").Select(y => new {
TRX_HD_Key = (int)y.Element(y.Name.Namespace + "TRX_HD_Key"),
Invoice_ID = (int)y.Element(y.Name.Namespace + "Invoice_ID"),
Date_DT = (DateTime)y.Element(y.Name.Namespace + "Date_DT"),
Merchant_Key = (int)y.Element(y.Name.Namespace + "Merchant_Key")
}).ToList();
}
}
}
I was trying to extract the data from pdf file using iTextSharp, but i go two errors. Actually I want to extract the data from pdf file and store it into database.
Here is my code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.util.collections;
using System.Linq;
public partial class frm_CreatePDF : System.Web.UI.Page
{
public string P_InputStream3 = "~/My Documents/List Of Holidays 2012";
protected void Page_Load(object sender, EventArgs e)
{
ExtractText();
}
private string ExtractText()
{
PdfReader reader = new PdfReader(Server.MapPath(P_InputStream3));
string txt = PdfTextExtractor.GetTextFromPage(reader, 2, new LocationTextExtractionStrategy());
return txt;
}
}
And the error is:
The name 'PdfTextExtractor' does not exist in the current context
The type or namespace name 'LocationTextExtractionStrategy
Make sure you have brought the namespace in which those two classes are defined into scope by adding the using directive:
using iTextSharp.text.pdf.parser;
I guess you need to use another namespace where LocationTextExtractionStrategy is defined
First of all you need to add a reference to the ItextSharp dll, after wich you can add a using statement to acces the namespace that contains that static class
http://msdn.microsoft.com/en-us/library/wkze6zky%28v=vs.100%29.aspx
This is a late answer, but I've found what your issue is:
You are missing the iTextSharp.text.pdf.parser using statement. Add this below your using iTextSharp.text.pdf; code:
using iTextSharp.text.pdf.parser;
That should be able to find your LocationTextExtractionStrategy pretty well.
You can also just add iTextSharp.text.pdf.parser directly behind your LocationTextExtractionStrategy if you want.
string txt = PdfTextExtractor.GetTextFromPage(reader, 2, new iTextSharp.text.pdf.parser.LocationTextExtractionStrategy());
But I would recommend the former. It's cleaner and easier to read.