remcl3.Wcl3Client client = new remcl3.Wcl3Client();
string rrs = client.getsql("sabatini", "ZXCqwe1920",112, w);
string xml = #rrs;
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlNodeList elemList = doc.GetElementsByTagName("AC_NO");
XmlNodeList elem = doc.GetElementsByTagName("AC_NAME");
for (int i = 0; i < elemList.Count; i++)
{
rem_no.DataTextField = doc.GetElementsByTagName("AC_NO").ToString();
rem_no.Items.Add(elemList[i].InnerXml);
rem_no.DataValueField = doc.GetElementsByTagName("AC_NAME").ToString();
rem_no.Items.Add(elem[i].InnerXml);
}
I'm trying to parse XML with two fields ac_no, ac_name using C# ASP.NET and put the data value, data text for the dropdownlist and bind the data value.
I have tried the following code but with no luck:
I want to bind the ac_no and show the ac_name; any help?
You can use a DataTable:
C# Code:
remcl3.Wcl3Client client = new remcl3.Wcl3Client();
string rrs = client.getsql("sabatini", "ZXCqwe1920",112, w);
string xml = #rrs;
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlNodeList elemList = doc.GetElementsByTagName("AC_NO");
XmlNodeList elem = doc.GetElementsByTagName("AC_NAME");
DataTable table = new DataTable();
table.Columns.Add("AC_NO");
table.Columns.Add("AC_NAME");
for (int i = 0; i < elemList.Count; i++)
{
DataRow row = table.NewRow();
var value = elemList[i].InnerXml;
var text = elem[i].InnerXml;
row["AC_NO"] = value;
row["AC_NAME"] = text;
table.Rows.Add(row);
}
// Configure your Dropdownlist
rem_no.DataSource = table;
rem_no.ValueMember = "AC_NO";
rem_no.DisplayMember = "AC_NAME";
rem_no.SelectedIndex = -1;
Or use a ListItem:
C# Code:
remcl3.Wcl3Client client = new remcl3.Wcl3Client();
string rrs = client.getsql("sabatini", "ZXCqwe1920",112, w);
string xml = #rrs;
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlNodeList elemList = doc.GetElementsByTagName("AC_NO");
XmlNodeList elem = doc.GetElementsByTagName("AC_NAME");
for (int i = 0; i < elemList.Count; i++)
{
var value = elemList[i].InnerXml;
var text = elem[i].InnerXml;
rem_no.Items.Add(new ListItem(text, value));
}
Related
Im trying to create a XML file with the next structure
<tdes1></tdes1>
<tdes2></tdes2>
<tdes3></tdes3>
but i am getting error when trying to append the elemnt
This is what i was trying to do
var xmlLlavesTDES = new XmlDocument();
xmlLlavesTDES.AppendChild(xmlLlavesTDES.CreateXmlDeclaration("1.0", "UTF-8", null));
// XElement test = new XElement("test");
for (var i = 0; i < this.llavesTDESArray.Length; i++)
{
var llavesTDESEncriptadas = encriptador.Encriptar(this.llavesTDESArray[i], this.llavePublicaEsclavo);
var llaveNum = i + 1;
XmlElement nodo= xmlLlavesTDES.CreateElement("tdes" + llaveNum);
nodo.InnerText = llavesTDESEncriptadas;
xmlLlavesTDES.AppendChild(nodo);
}
The error I get is This document already has a 'DocumentElement' node
i recommend an aproach similar to this using Linq instead of your approach:
//using System.Xml.Linq;
var xmlLlavesTDES = new XDocument();
XElement rootElement = new XElement("AllMyValues");
for (var i = 0; i < this.llavesTDESArray.Length; i++)
{
var llavesTDESEncriptadas = encriptador.Encriptar(this.llavesTDESArray[i], this.llavePublicaEsclavo);
var llaveNum = i + 1;
XElement nodo = new XElement("tdes" + llaveNum);
nodo.Value = llavesTDESEncriptadas;
rootElement.Add(nodo);
}
xmlLlavesTDES.Add(rootElement);
my code attempts to grab data from the RSS feed of a website. It grabs the nodes fine, but when attempting to grab the data from a node with a colon, it crashes and gives the error "Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function." The code is shown below:
WebRequest request = WebRequest.Create("http://buypoe.com/external.php?type=RSS2&lastpost=true");
WebResponse response = request.GetResponse();
StringBuilder sb = new StringBuilder("");
System.IO.StreamReader rssStream = new System.IO.StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8"));
XmlDocument rssDoc = new XmlDocument();
rssDoc.Load(rssStream);
XmlNodeList rssItems = rssDoc.SelectNodes("rss/channel/item");
for (int i = 0; i < 5; i++)
{
XmlNode rssDetail;
rssDetail = rssItems.Item(i).SelectSingleNode("dc:creator");
if (rssDetail != null)
{
user = rssDetail.InnerText;
}
else
{
user = "";
}
}
I understand that I need to define the namespace, but am unsure how to do this. Help would be appreciated.
You have to declare the dc namespace prefix using an XmlNamespaceManager before you can use it in XPath expressions:
XmlDocument rssDoc = new XmlDocument();
rssDoc.Load(rssStream);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(rssDoc.NameTable);
nsmgr.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");
XmlNodeList rssItems = rssDoc.SelectNodes("rss/channel/item");
for (int i = 0; i < 5; i++) {
XmlNode rssDetail = rssItems[i].SelectSingleNode("dc:creator", nsmgr);
if (rssDetail != null) {
user = rssDetail.InnerText;
} else {
user = "";
}
}
I have something like:
XmlDocument doc = new XmlDocument();
doc.Load(#"D:\filter.xml");
string filter1 = doc.SelectSingleNode("filter/f1").InnerText;
string filter2 = doc.SelectSingleNode("filter/f2").InnerText;
string filter3 = doc.SelectSingleNode("filter/f3").InnerText;
string filter4 = doc.SelectSingleNode("filter/f4").InnerText;
string filter5 = doc.SelectSingleNode("filter/f5").InnerText;
string filter6 = doc.SelectSingleNode("filter/f6").InnerText;
And so on...My question is how could I generate these strings in a loop?something like.
XmlDocument doc = new XmlDocument();
doc.Load(#"D:\filter.xml");
for (int i = 0; i < 7; i++)
{
string filter + i = doc.SelectSingleNode("filter/f" + i).InnerText;;
}
Fill a List<string>:
List<string> filterList = new List<string>();
for (int i = 0; i < 7; i++)
{
filterList.Add(doc.SelectSingleNode("filter/f" + i).InnerText);
}
Now you can access them via index, f.e. filter 5:
string filter5 = filterList[4]; // zero based
You want to use a collection, like List<string>:
XmlDocument doc = new XmlDocument();
doc.Load(#"D:\filter.xml");
var myList = new List<strinig>;
for (int i = 0; i < 7; i++)
{
myList.Add(doc.SelectSingleNode("filter/f" + i).InnerText);
}
Then you can use the list by referencing a string's index:
myValue = myList[3];
Use Collection to store String and this is how you can add string to collection from the XML.
doc.Load(#"D:\filter.xml");
List<string> filter = new List<string>();
foreach (XmlNode item in doc.SelectSingleNode("filter").ChildNodes)
{
filter.Add(item.InnerText.ToString());
}
Hope this helps.
I have a sqlite database which has 7 tables. I need to export them to an XML file in c#.net.
When doing so, i have used 7 loops nested to access the data of 7 Datatables to maintain the dependencies of columns. but while i run it, my machine becomes very slow. it generates XML file, but the generated XML changes after some time. it seems, in every iteration , it generates one XML and replace it by new one. my codes are following :
public XMLSerializer(String filepath,ROOT root)
{
XmlDocument doc = new XmlDocument();
XmlNode rootNode, aNode, pNode, eNode, mNode, plNode;
// Create a procesing instruction.
XmlProcessingInstruction newPI;
String PItext = "type='text/xsl' href='root.xsl'";
newPI = doc.CreateProcessingInstruction("xml-stylesheet", PItext);
// Add the processing instruction node to the document.
doc.AppendChild(newPI);
rootNode = doc.CreateElement("root");
doc.AppendChild(rootNode);
plNode = doc.CreateElement("pl");
rootNode.AppendChild(plNode);
eNode = doc.CreateElement("e");
rootNode.AppendChild(eNode);
mNode = doc.CreateElement("m");
rootNode.AppendChild(mNode);
aNode = doc.CreateElement("a");
rootNode.AppendChild(aNode);
DataTable plDT = root.Getpl();
foreach (DataRow pRow in plDT.Rows)
{
XmlNode plNode = doc.CreateElement("p");
XmlNode pIDNode = doc.CreateElement("id");
XmlNode pteNode = doc.CreateElement("te");
XmlNode fNode = doc.CreateElement("fs");
plNode.AppendChild(pIDNode);
plNode.AppendChild(pteNode);
plNode.AppendChild(fNode);
plNode.AppendChild(plNode);
pIDNode.InnerText = pRow["id"].ToString();
pteNode.InnerText = root.Getpte(pRow["id"].ToString());
DataTable fmdt = root.GetFml(pRow["id"].ToString());
foreach (DataRow fmRow in fmdt.Rows)
{
XmlNode fsNode = doc.CreateElement("fm");
XmlNode fmIDNode = doc.CreateElement("id");
XmlNode fmpNode = doc.CreateElement("p");
XmlNode eNode = doc.CreateElement("e");
fsNode.AppendChild(fmIDNode);
fsNode.AppendChild(fmpNode);
fsNode.AppendChild(eNode);
plNode.AppendChild(fsNode);
fmIDNode.InnerText = fmRow["id"].ToString();
fmpNode.InnerText = pRow["id"].ToString();
DataTable eDT = root.Gete();
foreach (DataRow eRow in eDT.Rows)
{
XmlNode eNode = doc.CreateElement("e");
XmlNode eIDNode = doc.CreateElement("id");
XmlNode eDanNode = doc.CreateElement("dan");
XmlNode mNode = doc.CreateElement("m");
eNode.AppendChild(eIDNode);
eNode.AppendChild(edanNode);
//eNode.AppendChild(eNode);
fsNode.AppendChild(eNode);
eIDNode.InnerText = eRow["id"].ToString();
edanNode.InnerText = root.Getedan(eRow["id"].ToString());
DataTable mDT = root.Getm();
foreach (DataRow mRow in mDT.Rows)
{
XmlNode mNode = doc.CreateElement("m");
XmlNode mIDNode = doc.CreateElement("id");
XmlNode meNode = doc.CreateElement("e");
mNode.AppendChild(mIDNode);
mNode.AppendChild(meNode);
eNode.AppendChild(mNode);
mIDNode.InnerText = mRow["id"].ToString();
meNode.InnerText = root.Getme(mRow["id"].ToString());
DataTable aDT = root.Geta();
foreach (DataRow aRow in aDT.Rows)
{
XmlNode aNode = doc.CreateElement("a");
XmlNode AssIDNode = doc.CreateElement("id");
XmlNode AssNaNode = doc.CreateElement("na");
XmlNode pinNode = doc.CreateElement("pin");
aNode.AppendChild(AssIDNode);
aNode.AppendChild(AssNaNode);
aNode.AppendChild(pinNode);
AssIDNode.InnerText = aRow["id"].ToString();
AssNaNode.InnerText = aRow["na"].ToString();
mNode.AppendChild(aNode);
DataTable piDT = root.Getp(aRow["id"].ToString());
foreach (DataRow piRow in piDT.Rows)
{
XmlNode pInNode = doc.CreateElement("pIn");
XmlNode pInIDNode = doc.CreateElement("id");
XmlNode pInaNode = doc.CreateElement("a");
XmlNode pInpNode = doc.CreateElement("p");
XmlNode pInFminNode = doc.CreateElement("fmin");
pInNode.AppendChild(pInIDNode);
pInNode.AppendChild(pInaNode);
pInNode.AppendChild(pInpNode);
pInNode.AppendChild(pInFminNode);
aNode.AppendChild(pInNode);
pInIDNode.InnerText = piRow["id"].ToString();
pInaNode.InnerText = aRow["id"].ToString();
pInpNode.InnerText = pRow["id"].ToString();
DataTable fmiDT = root.GetFmin(piRow["id"].ToString());
foreach (DataRow fmiRow in fmiDT.Rows)
{
XmlNode fmInNode = doc.CreateElement("fmIn");
XmlNode fmInIDNode = doc.CreateElement("id");
XmlNode fmInpInNode = doc.CreateElement("pIn");
XmlNode fmInFmNode = doc.CreateElement("fm");
XmlNode fmIneNode = doc.CreateElement("e");
XmlNode fmInmNode = doc.CreateElement("m");
fmInNode.AppendChild(fmInIDNode);
fmInNode.AppendChild(fmInpInNode);
fmInNode.AppendChild(fmInFmNode);
fmInNode.AppendChild(fmIneNode);
fmInNode.AppendChild(fmInmNode);
pInNode.AppendChild(fmInNode);
fmInIDNode.InnerText = fmiRow["id"].ToString();
fmInpInNode.InnerText = piRow["id"].ToString();
fmInFmNode.InnerText = fmRow["id"].ToString();
fmIneNode.InnerText = eRow["id"].ToString();
fmInmNode.InnerText = mRow["id"].ToString();
}
}
}
}
}
}
}
doc.Save(filepath);
}
}
}
i think my way of coding is not efficient. could anyone please show me any better approach of accessing the datatables ?
you can simply use DataTable.WriteXml() :
YourDataTable.WriteXml(strfilepath);
or
YourDataSet.WrteXml(strfilepath);
I need to store the element values which are inside the nodes "member" . I have tried the following code but I can't achieve it. How to get the values. Any help would be appreciated
XML:
<ListInventorySupplyResponse xmlns="http://mws.amazonaws.com/FulfillmentInventory/2010-10-01/">
<ListInventorySupplyResult>
<InventorySupplyList>
<member>
<SellerSKU>043859634910</SellerSKU>
<FNSKU>X000IA4045</FNSKU>
<ASIN>B005YV4DJO</ASIN>
<Condition>NewItem</Condition>
<TotalSupplyQuantity>7</TotalSupplyQuantity>
<InStockSupplyQuantity>7</InStockSupplyQuantity>
<EarliestAvailability>
<TimepointType>Immediately</TimepointType>
</EarliestAvailability>
<SupplyDetail>
</SupplyDetail>
</member>
</InventorySupplyList>
</ListInventorySupplyResult>
<ResponseMetadata>
<RequestId>58c9f4f4-6f60-496a-8d71-8fe99ce301c9</RequestId>
</ResponseMetadata>
</ListInventorySupplyResponse>
C# Code:
string a = Convert.ToString(oInventorySupplyRes.ToXML());
XmlDocument oXdoc = new XmlDocument();
oXdoc.LoadXml(a);
XmlNodeList oInventorySupplyListxml = oXdoc.SelectNodes("//member");
foreach (XmlNode itmXml in oInventorySupplyListxml)
{
// var cond = itmXml.InnerXml.ToString();
var asinVal = itmXml.SelectSingleNode("ASIN").Value;
var TotalSupplyQuantityVal = itmXml.SelectSingleNode("TotalSupplyQuantity").Value;
}
ResultView : "Enumeration yielded no results" and count = 0;
Edit 1:
string a = Convert.ToString(oInventorySupplyRes.ToXML());
var status = oInventorySupplyResult.InventorySupplyList;
XmlDocument oXdoc = new XmlDocument();
var doc = XDocument.Parse(a);
var r = doc.Descendants("member")
.Select(member => new
{
ASIN = member.Element("ASIN").Value,
TotalSupplyQuantity = member.Element("TotalSupplyQuantity").Value
});
private string mStrXMLStk = Application.StartupPath + "\\Path.xml";
private System.Xml.XmlDocument mXDoc = new XmlDocument();
mXDoc.Load(mStrXMLStk);
XmlNode XNode = mXDoc.SelectSingleNode("/ListInventorySupplyResult/InventorySupplyList/member");
if (XNode != null)
{
int IntChildCount = XNode.ChildNodes.Count;
for (int IntI = 1; IntI <= IntChildCount ; IntI++)
{
string LocalName = XNode.ChildNodes[IntI].LocalName;
XmlNode Node = mXDoc.SelectSingleNode("/Response/" + LocalName);
// Store Value in Array assign value by "Node.InnerText"
}
}
Try This Code. Its Worked
try using this xpath
string xPath ="ListInventorySupplyResponse/ListInventorySupplyResult
/InventorySupplyList/member"
XmlNodeList oInventorySupplyListxml = oXdoc.SelectNodes(xpath);
when you do "//member", then, the code is trying to look for element named member from the root level, which is not present at the root level, rather it is nested beneath few layers.
I think this will help you..
string a = Convert.ToString(oInventorySupplyRes.ToXML());
XmlDocument oXdoc = new XmlDocument();
oXdoc.LoadXml(a);
XmlNodeList fromselectors;
XmlNodeList toselectors;
XmlElement root = oXdoc.DocumentElement;
fromselectors = root.SelectNodes("ListInventorySupplyResult/InventorySupplyList/member/ASIN");
toselectors = root.SelectNodes("ListInventorySupplyResult/InventorySupplyList/member/TotalSupplyQuantity");
foreach (XmlNode m in fromselectors)
{
you will have value in `m.InnerXml` use it whereever you want..
}
foreach (XmlNode n in toselectors)
{
you will have value in `n.InnerXml` use it whereever you want..
}