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);
Related
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));
}
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 a Xamarin (C#) project, where I am trying to loop through some XML, but for some reason my code is not working.
This is what I have now:
DeviceList = new List<DeviceInfo>();
string ResultStatus = "";
string ResultDevice = "";
var result = Encoding.Default.GetString(e.Result);
result = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + result;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(result);
string xPathStatus = "ed_listdevices";
var nodes = xmlDoc.SelectNodes(xPathStatus);
foreach (XmlNode xNode in nodes) {
ResultStatus = xNode.SelectSingleNode("//status").InnerText;
ResultDevice = xNode.SelectSingleNode("//device").InnerText;
}
if (ResultStatus.ToLower() == "ok") {
XmlDocument deviceDoc = new XmlDocument();
deviceDoc.LoadXml(result);
var deviceNodes = deviceDoc.SelectNodes(xPathStatus + "/device");
//foreach(XmlNode dNode in deviceNodes) {
for (int i = 0; i < deviceNodes.Count; i++) {
DeviceList.Add(new DeviceInfo() {
DeviceID = deviceNodes[i].SelectSingleNode("//id").InnerXml,
DeviceName = deviceNodes[i].SelectSingleNode("//name").InnerXml,
DeviceExtraName = "",
DeviceOnlineStatus = deviceNodes[i].SelectSingleNode("//status").InnerXml,
Location = deviceNodes[i].SelectSingleNode("//address").InnerXml,
Time = deviceNodes[i].SelectSingleNode("//time").InnerXml
});
}
}
When I step though the code I get the "ResultStatus" and "ResultDevice" correctly, and when I get to the
for (int i = 0; i < deviceNodes.Count; i++)
the "deviceNodes" variable have a count of 91, and I can see all the individual xml elements that I am suppose to get from the webservice I am calling.
However, when I loop through deviceNodes[i] I only get values from the very first XML element (yes, the value of "i" does change). In other words, my DeviceList is filled with 91 entries with the same values.
Am I missing something obvious?? What am I doing wrong since this isn't working??
PS: I have also tried using a foreach (XmlNode node in deviceNodes) but the result was the same.
"//" in the selector tells it to search from the root node of the document. If you want to search locally under the "current" node, remove the "//"
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..
}
I have the following XML :
<LOCALCELL_V18 ID = "0x2d100000">
<MXPWR ID = "0x3d1003a0">100</MXPWR>
</LOCALCELL_V18>
<LOCALCELL_V18 ID = "0x2d140000">
<MXPWR ID = "0x3d1403a0">200</MXPWR>
</LOCALCELL_V18>
<LOCALCELL_V18 ID = "0x2d180000">
<MXPWR ID = "0x3d1803a0">300</MXPWR>
</LOCALCELL_V18>
I want to get the inner text of each <MXPWR>. however, it is not allowed to use ID# to locate the inner text since it is not always the same. here is my code:
XmlNodeList LocalCell = xmlDocument.GetElementsByTagName("LOCALCELL_V18");
foreach (XmlNode LocalCell_Children in LocalCell)
{
XmlElement MXPWR = (XmlElement)LocalCell_Children;
XmlNodeList MXPWR_List = MXPWR.GetElementsByTagName("MXPWR");
for (int i = 0; i < MXPWR_List.Count; i++)
{
MaxPwr_form_str = MXPWR_List[i].InnerText;
}
}
Any opinion will be appreciated.
I would use xpath. It was designed for just this sort of problem. Something like:
using System.Xml;
using System.Xml.XPath;
....
string fileName = "data.xml"; // your file here
XPathDocument doc = new XPathDocument(fileName);
XPathNavigator nav = doc.CreateNavigator();
// Compile an xpath expression
XPathExpression expr = nav.Compile("./LOCALCELL_V18/MXPWR");
XPathNodeIterator iterator = nav.Select(expr);
// Iterate on the node set
while (iterator.MoveNext())
{
string s = iterator.Current.Value;
}
When I run this on your XML file (wrapped in a root node) I get:
s = 100
s = 200
s = 300
I would use XLinq:
using System.Xml.Linq;
var xDoc = XDocument.Load("data.xml");
var mxpwrs = xDoc.Descendants("MXPWR");
foreach (var mxpwr in mxpwrs)
{
Console.WriteLine(mxpwr.Value);
}