How to get attribute in the XDocument object
You need to get the attribute of the <audio> element: string value = xdoc.Root.Element(“audio”).Attribute(“first”).Value;
You need to get the attribute of the <audio> element: string value = xdoc.Root.Element(“audio”).Attribute(“first”).Value;
StringWriter advertises itself as using UTF-16. It’s easy to fix though: public class Utf8StringWriter : StringWriter { public override Encoding Encoding { get { return Encoding.UTF8; } } } That should be enough in your particular case. A rather more well-rounded implementation would: Have constructors matching those in StringWriter Allow the encoding to be specified … Read more
Just pass the XElement to the constructor of XDocument: var xdoc = new XDocument(new XElement(“a”, “b”));
It looks like you’re attempting to load an XML file into an XDocument, but to do so you need to call XDocument.Load(“C:\\temp\\contacts.xml”); – you can’t pass an XML file into the constructor. You can also load a string of XML with XDocument.Parse(stringXml);. Change your first line to: var doc = XDocument.Load(“c:\\temp\\contacts.xml”); And it will work. … Read more
XElement (actually XObject) implements the IXmlLineInfo interface, so you can use it: IXmlLineInfo info = category; int lineNumber = info.LineNumber; Note that line information is not always available, you need to call the HasLineInfo method to check if the information is available. You can specify LoadOptions.SetLineInfo when you load the document with XDocument.Load
Do you mean: new XAttribute(XNamespace.Xmlns + “ns4”, ns4), new XAttribute(XNamespace.Xmlns + “ns3”, ns3), new XAttribute(XNamespace.Xmlns + “ns2”, ns2),
test.xml: <?xml version=”1.0″ encoding=”utf-8″?> <Contacts> <Node> <ID>123</ID> <Name>ABC</Name> </Node> <Node> <ID>124</ID> <Name>DEF</Name> </Node> </Contacts> Select a single node: XDocument XMLDoc = XDocument.Load(“test.xml”); string id = “123”; // id to be selected XElement Contact = (from xml2 in XMLDoc.Descendants(“Node”) where xml2.Element(“ID”).Value == id select xml2).FirstOrDefault(); Console.WriteLine(Contact.ToString()); Delete a single node: XDocument XMLDoc = XDocument.Load(“test.xml”); string id … Read more
The configuration element is in the unnamed namespace, and the MyNode is bound to the lcmp namespace without a namespace prefix. This XPATH statement will allow you to address the MyNode element without having declared the lcmp namespace or use a namespace prefix in your XPATH: /configuration/*[namespace-uri()=’lcmp’ and local-name()=’MyNode’] It matches any element that is … Read more
Try this: var x = new XElement(“root”, new XElement(“name”, “AAA”), new XElement(“last”, “BBB”), new XElement(“children”, from c in family select new XElement(“child”, new XElement(“name”, “XXX”), new XElement(“last”, “TTT”) ) ) );
It’s probably just worth catching the specific exception if you want to show a message to the user: try { XDocument xd1 = new XDocument(); xd1 = XDocument.Load(myfile); } catch (XmlException exception) { ShowMessage(“Your XML was probably bad…”); }