|
Hi,
I have been looking at msdn samples.
It seems I am having problems with .Append
How do I append something like:
<column>
<ElementName>654322</ElementName>
<ElementValue>xyz</ElementValue>
</column>
|
|
|
|
|
You have to create a new node column. Then create nodes for elementname and elementvalue. Append these nodes to the column node. Last step is to append the column node to the node in your document.
Btw, if you want to create nodes, use the CreateNode method of your XmlDocument.
|
|
|
|
|
Not sure what you mean.
How is this possible to tell the system where exactly in the hierarchy to place this elelemnt within the file?
Are you able to write a test sample based on the xml I gave before?
Thanks
|
|
|
|
|
Well I will try (without intellisense)...
private void ModifyXML()
{
XmlDocument doc = new XmlDocument();
doc.Load(strPath);
XmlNodeList nodeList = doc.GetElementsByTagName("ElementName");
foreach (XmlNode node in nodeList)
{
string strValue = node.ChildNodes[0].Value.ToString();
if (strValue.ToLower() == "654322")
{
ListBox1.Items.Add(node.OuterXml);
ListBox1.Items.Add(node.NextSibling.OuterXml);
ListBox1.Items.Add("");
XmlNode columnNode = doc.CreateElement("column");
XmlNode elementNameNode = doc.CreateElement("elementName");
XmlNode elementValueNode = doc.CreateElement("elementValue");
elementNameNode.InnerText = "foo";
elementValueNode.InnerText = "bar";
columnNode.AppendChild(elementNameNode);
columnNode.AppendChild(elementValueNode);
node.ParentNode.AppendChild(columnNode);
}
}
}
|
|
|
|
|
Hi,
I tried it with a bit of adjustments.
But I get this:
<column>
<ElementName>1234</ElementName>
<ElementValue>uyt</ElementValue>
<column>
<ElementName>newKey</ElementName>
<ElementValue>newValue</ElementValue>
</column>
</column>
instread of:
<column>
<ElementName>1234</ElementName>
<ElementValue>uyt</ElementValue>
<column>
<column>
<ElementName>newKey</ElementName>
<ElementValue>newValue</ElementValue>
</column>
-----------
This is the code:
Thanks
private void LoadXMLmodify()
{
XmlDocument doc = new XmlDocument();
doc.Load(strPath);
// Retrieve all prices.
XmlNodeList nodeList = doc.GetElementsByTagName("mapKey");
foreach (XmlNode node in nodeList)
{
string strValue = node.ChildNodes[0].Value.ToString();
if (strValue.ToLower() == "0024007")
{
if (node.NextSibling.InnerXml.Trim() == "1")
{
string strMapValue = node.NextSibling.InnerXml.Trim();
//Remove element...
//Debug.WriteLine("mapKey : " + strValue + " and mapValue : " + strMapValue + " removed.");
//node.RemoveChild(node.FirstChild);
//Add element...
XmlNode columnNode = doc.CreateElement("Mapping");
XmlNode elementKeyNode = doc.CreateElement("mapKey");
XmlNode elementValueNode = doc.CreateElement("mapValue");
elementKeyNode.InnerText = strValue;
elementValueNode.InnerText = "999";
columnNode.AppendChild(elementKeyNode);
columnNode.AppendChild(elementValueNode);
node.ParentNode.AppendChild(columnNode);
//string strMapValue = node.NextSibling.InnerText;
//Debug.WriteLine("mapValue changed from : " + strMapValue + " to : " + "newValue");
//node.NextSibling.InnerText = "newVlaue";
//doc.Save(strPath);
}
}
}
doc.Save(strPath);
}
|
|
|
|
|
okay, try node.parentnode.parentnode
You are appending the new column node to an existing column node, but it must be the node above the existing column node.
|
|
|
|
|
Error:
In my code I sent you it gives an error on the line:
foreach(XmlNode node in nodeList)
It says: The element list has changed. The enumeration operation failed to continue.
Thanks
|
|
|
|
|
As the error message is saying a list can not be changed during the iteration. Convert the XmlNodeList to an array and use this array to iterate through the nodes.
|
|
|
|
|
The xml has many many elements and subelements...
How can This be converted to array?
And may be I should not be in the loop to save?
Thanks
|
|
|
|
|
Build a new list or array containing the elements of the XmlNodeList.
List<XmlNode> myNodes = new List<XmlNode>(doc.GetElementsByTagName("ElementName"));
Then iterate through myNodes.
|
|
|
|
|
I guess I do not have to be in the loop when I add a new row?
Don't you think?
|
|
|
|
|
Yes that is correct. But sometimes the logic just needs to be done within a loop. If you can avoid doing all this within the loop -> do it.
|
|
|
|
|
I have modified the loop so that as soon as there is an addition to the xml, then save the file and then break; this way it comes out of the loop.
Question:
I can see the xml I added is in there, so I now try to remove it as follows:
Do you see what is wrong here because i get an error on line with ????? it says object reference not set to an instance of an object.
Not that this error does not always happen. It seems I have to close the file and then re-open it to work correctly. Is this the way it should be?
Thanks
private void LoadXMLRemove()
{
XmlDocument doc = new XmlDocument();
doc.Load(strPath);
// Retrieve all .
XmlNodeList nodeList = doc.GetElementsByTagName("mapKey");
foreach (XmlNode node in nodeList)
{
//?????
string strValue = node.ChildNodes[0].Value.ToString();
//???????
if (strValue.ToLower() == "0024007")
{
if (node.NextSibling.InnerXml.Trim() == "1")
{
string strMapValue = node.NextSibling.InnerXml.Trim();
//Remove element...
Debug.WriteLine("mapKey : " + strValue + " and mapValue : " + strMapValue + " removed.");
node.RemoveChild(node.FirstChild);
doc.Save(strPath);
break;
}
}
}
}
modified on Tuesday, October 13, 2009 8:05 AM
|
|
|
|
|
It seems that the node you doesn't have a childnode. Did you try to debug?
|
|
|
|
|
this error does not always happen. It seems I have to close the file and then re-open it to work correctly. Is this the way it should be?
Thanks
|
|
|
|
|
hi,
This list requires int
How is this code corrected please?
Thanks
|
|
|
|
|
|
Hi,
Any way, I am now using a string such that i transfer the xml into a string, then replace, delete what I want.
This seems to work.
Do you think it is ok to use a string as explained?
Thanks
|
|
|
|
|
Please note that I do not want to use xpath or linq or database. Just the xmlDocument for business reasons Why? I can understand you don't want to upgrade to .NET 3.5 to use LINQ, but SelectNodes (that uses XPath to get child nodes) is always been available in XmlDocument.
Said that, look at the XmlNode members, and you will find these two methods: AppendChild and RemoveChild.
|
|
|
|
|
hi,
i am using a grid that have its own print method.
i am using in my application this grid several times.
i want to print all the grids in 1 time (i can do grid1.print , grid2.print but than i can't preview them both in one time)
is there any way to make a virual pritner that all the prints will go to it and than i wiil be able to preview it once and see all the output(and select copies,and the real printer that i want to print to)?
thanks,
Samy
|
|
|
|
|
You could create a PrintDocument and print your grids one per page, or if they are small enough fit two or more onto the page. You will then be able to preview them all at once.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
how can a PrintDocument be used in this case?
my grid prints automatically to the default printer(i can change the printer name to another printer not the difault and it works but a PrintDocument dosn't have a "printer name" to pretend.
thanks,
Samy
|
|
|
|
|
If you have the source code for the grid you are using, then modify it (add a method that prints to a Graphics instance).
If you do not have the code then I would look for an alternative grid that gives you the source. There are plenty of them, just google for c# print datagridview.
Other than that I have no idea.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Hello,
I want to change the maxvalue of the scrollbar in a treeview. So that I can see the whole treenode.
It is in winforms.
Can anyone help me to achieve this?
Thanks,
|
|
|
|
|
Do you mean that you cannot scroll to the bottom of the tree nodes (vertically)?
Could you post some code where this problem shows?
|
|
|
|