XML and .NET 1.1

C

Christian Havel

Hi,

I have a string (C#) which contains XML in the following format:

<entity="MyType" date="10.04.2007" furtherInformation="moreInfos">
<item name="MyName" value="MyValue"/>
<!-- hier können noch beliebig viele weitere Einträge vorkommen -->
</entity>

I want to save this string to a eventually already existing XML file.
If the file already contains any data the new data should be added at the
bottom.
The result should look like the following:

<entities>
<entity="MyType1" date="10.04.2007" furtherInformation="moreInfos">
<item name="MyName" value="MyValue"/>
</entity>
<entity="MyType2" date="10.04.2007" furtherInformation="moreInfos">
<item name="MyName" value="MyValue"/>
</entity>
--> Add further data here <--
</entities>

How can I do this using C# and .NET 1.1?
Christian
 
M

Martin Honnen

Christian said:
I have a string (C#) which contains XML in the following format:

<entity="MyType" date="10.04.2007" furtherInformation="moreInfos">
<item name="MyName" value="MyValue"/>
<!-- hier können noch beliebig viele weitere Einträge vorkommen -->
</entity>

I want to save this string to a eventually already existing XML file.
If the file already contains any data the new data should be added at the
bottom.
The result should look like the following:

<entities>
<entity="MyType1" date="10.04.2007" furtherInformation="moreInfos">
<item name="MyName" value="MyValue"/>
</entity>
<entity="MyType2" date="10.04.2007" furtherInformation="moreInfos">
<item name="MyName" value="MyValue"/>
</entity>
--> Add further data here <--
</entities>

How can I do this using C# and .NET 1.1?

Use System.Xml.XmlDocument e.g.
XmlDocument doc = new XmlDocument();
doc.Load("file.xml"); // file.xml is your existing XML document

XmlDocumentFragment docFrag = doc.CreateDocumentFragment();
docFrag.InnerXml = yourString;
doc.DocumentElement.AppendChild(docFrag);
doc.Save("file.xml");

It is not clear what you want to do with the string if the file does not
exist so that C# snippet above ony shows what to do if the file exists.
 
J

Jon Skeet [C# MVP]

Christian Havel said:
I have a string (C#) which contains XML in the following format:

I want to save this string to a eventually already existing XML file.
If the file already contains any data the new data should be added at the
bottom.
The result should look like the following:

How can I do this using C# and .NET 1.1?

Create a new XmlDocument, then call Load to load the existing file.
Create a new element, possibly by creating another XmlDocument and
calling LoadXML, then importing the element into the first document at
the right position .Then save the first document out again.
 
C

Christian Havel

Hi Martin,

if the file does not exists it should be created.
Do you have one more tip for me, how to do this?
Thanks
Christian
 
M

Martin Honnen

Christian said:
if the file does not exists it should be created.
Do you have one more tip for me, how to do this?

XmlDocument doc = new XmlDocument();
string fileName = "file.xml";
if (!File.Exists(fileName))
{
doc.AppendChild(doc.CreateElement("entities"));
}
else {
doc.Load(fileName);
}
XmlDocumentFragment docFrag = doc.CreateDocumentFragment();
docFrag.InnerXml = yourString;
doc.DocumentElement.AppendChild(docFrag);
doc.Save(fileName);
 
C

Christian Havel

Martin, I love you ;-)

Well, can you tell me how I can delete a document fragment when I find it by
a unique value?

<entity created="..." someOtherStuff="..">
<item NAME="ID" VALUE="ThisIsAUniquieId"/> <= key
</entity>
<entity created="..." someOtherStuff="..">
<item NAME="ID" VALUE="ThisIsAnotherUniquieId"/>
</entity>

Christian
 
M

Martin Honnen

Christian said:
Well, can you tell me how I can delete a document fragment when I find it by
a unique value?

<entity created="..." someOtherStuff="..">
<item NAME="ID" VALUE="ThisIsAUniquieId"/> <= key
</entity>

You might want to try an XPath tutorial, then you can use the method
SelectSingleNode to select any node with the power of XPath expressions.
Once you have found an XmlNode node you simply delete it with
node.ParentNode.RemoveChild(node);

So assuming you want to find and delete the entity containing the item
you can do e.g.
XmlNode entity = doc.SelectSingleNode(
"/entities/entity[item/@VALUE = 'ThisIsAUniquieId']");
if (entity != null) {
entity.ParentNode.RemoveChild(entity);
}

If you want to find the item itself and delete it then you need a
different XPath
XmlNode item = doc.SelectSingleNode(
"entities/entity/item[@Value = 'ThisIsAUniquieId']");
if (item != null) {
item.ParentNode.RemoveChild(item);
}
 
M

Marc Gravell

If you have an XmlNode (from SelectSingleChild or similar) then you
can call:

node.ParentNode.RemoveChild(node);

The exact query to SelectSingleChild depends on which node you want to
remove, and the location. To remove the "item" you could use something
like:

XmlNode node = doc.SelectSingleNode("/*/entity/
item[@VALUE='ThisIsAUniquieId']");

Marc
 
M

Marc Gravell

I just realised that your "item" record looks like a name/value lookup
- in which case you probably want to delete to entity - so something
like:
"/*/entity[item[NAME='ID' and @VALUE='ThisIsAUniquieId']]"

For reference, this apprach makes life hard; it would be easier to
have
<entity ... id="ThisIsAUniquieId">...</entity>

Then you could use
"/*/entity[@id='ThisIsAUniquieId']"
Plus it would be easier to index if needed

Marc
 
M

Marc Gravell

"/*/entity[item[NAME='ID' and @VALUE='ThisIsAUniquieId']]"
Should have been @NAME

Marc
 
B

Blip

Christian said:
Well, can you tell me how I can delete a document fragment when I find it by
a unique value?

<entity created="..." someOtherStuff="..">
<item NAME="ID" VALUE="ThisIsAUniquieId"/> <= key
</entity>

You might want to try an XPath tutorial, then you can use the method
SelectSingleNode to select any node with the power of XPath expressions.
Once you have found an XmlNode node you simply delete it with
node.ParentNode.RemoveChild(node);

So assuming you want to find and delete the entity containing the item
you can do e.g.
XmlNode entity = doc.SelectSingleNode(
"/entities/entity[item/@VALUE = 'ThisIsAUniquieId']");
if (entity != null) {
entity.ParentNode.RemoveChild(entity);
}

If you want to find the item itself and delete it then you need a
different XPath
XmlNode item = doc.SelectSingleNode(
"entities/entity/item[@Value = 'ThisIsAUniquieId']");
if (item != null) {
item.ParentNode.RemoveChild(item);
}

Martin...

Can you or anyone recommend a good xpath tutorial? Obviously, I could
google for it - just want to know what this group's braintrust
recommends...

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top