Writing to an xml file
i was wondering if i am adopting the correct approach to write to an xml file because when i view the file after this the changes are not present
XmlDocument doc = new XmlDocument();
doc.Load("pubs.xml");
XmlElement root = doc.DocumentElement;
XmlElement pub = doc.CreateElement("Pub");
XmlElement name = doc.CreateElement("Name");
XmlElement address = doc.CreateElement("Address");
XmlElement postCode = doc.CreateElement("PostCode");
XmlElement score = doc.CreateElement("PubScore");
name.InnerText = txtName.Text;
address.InnerText = txtAddress.Text;
postCode.InnerText = txtPc.Text;
score.InnerText = txtPs.Text;
pub.AppendChild(name);
pub.AppendChild(address);
pub.AppendChild(postCode);
pub.AppendChild(score);
root.AppendChild(pub);
doc.Save("pubs.xml");
This, in the root of the document should write an element like
<Pub>
<Name>xxx</Name>
<Address>xxx</Address>
<OnlineRating>xxx</OnlineRating>
<PostCode>xxx</PostCode>
</Pub>
attempted change :
private void btnAdd_Click(object sender, EventArgs e)
{
XDocument doc = new XDocument(
new XElement("Pub",
开发者_StackOverflow new XElement("Name", txtName.Text),
new XElement("Addresss", txtAddress.Text),
new XElement("OnlineRating", txtPs.Text),
new XElement("PostCode", txtPc.Text),
)
);
doc.Save("pubs.xml");
}
If you're using .NET 3.5 or above, then you could write that as:
XDocument doc = new XDocument(
new XElement("Pub",
new XElement("Name", "xxx"),
new XElement("Addresss", "xxx"),
new XElement("OnlineRating", "xxx"),
new XElement("PostCode", "xxx")
)
);
doc.Save("pubs.xml");
And you're done!
Similarly, if you want the XML as:
<Pub>
<Name FirstName="Xyz" SecondName="Abc" />
<PostCode Code="8787"/>
</Pub>
Then you can do this instead:
XDocument doc = new XDocument(
new XElement("Pub",
new XElement("Name", new XAttribute("FirstName", "Xyz"), new XAttribute("SecondName", "Abc")),
new XElement("PostCode", new XAttribute("Code", "8787"))
)
);
doc.Save("pubs.xml");
If you want to load an existing XML file, and you want to append these to that file, then do this:
XDocument doc = XDocument.Load("pubs.xml"); //load the existing file!
//add one element with its descendants
doc.Add(new XElement("Pub",
new XElement("Name", "xxx"),
new XElement("Addresss", "xxx"),
new XElement("OnlineRating", "xxx"),
new XElement("PostCode", "xxx")
)
);
doc.Save("pubs.xml"); //save the whole document!
May be completely wrong but have you tried changing:
pub.AppendChild(name);
pub.AppendChild(address);
pub.AppendChild(postCode);
pub.AppendChild(score);
root.AppendChild(pub);
to
root.AppendChild(pub);
pub.AppendChild(name);
pub.AppendChild(address);
pub.AppendChild(postCode);
pub.AppendChild(score);
Just because that makes more logical sense to me...
Just executing your code snipped on "pubs.xml" that looks like this...
<SomeElement>
<SomeTag1/>
<SomeTag2/>
<SomeTag3/>
</SomeElement>
...produces the following "pubs.xml"...
<SomeElement>
<SomeTag1 />
<SomeTag2 />
<SomeTag3 />
<Pub>
<Name>name</Name>
<Address>address</Address>
<PostCode>post_code</PostCode>
<PubScore>score</PubScore>
</Pub>
</SomeElement>
...which, if I understand you correctly, is what you want (you said: "..in the root of the document should write an element like...").
So whatever is causing your problem is outside the code snipped you have shown us.
Are you sure the doc.Save("pubs.xml")
is succeeding and not throwing an exception (e.g. due read-only flag)?
Also, are you sure you are viewing the right "pubs.xml"? Perhaps you have more than one copy and you happen to look at the wrong one? Look at doc.BaseURI
to confirm the actual location of the file being written to.
精彩评论