
How to: Load XML from a File, String, or Stream (Visual Basic)
You can create XML Literals and populate them with the contents from an external source such as a file, a string, or a stream by using several methods. These methods are shown in the following examples.
Note
Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Personalizing the IDE.
To load XML from a file
To populate an XML literal such as an XElement or XDocument object from a file, use the method. This method can take a file path, text stream, or XML stream as input.
The following code example shows the use of the Load(String) method to populate an XDocument object with XML from a text file.
To load XML from a string
To populate an XML literal such as an XElement or XDocument object from a string, you can use the method.
The following code example shows the use of the XDocument.Parse(String) method to populate an XDocument object with XML from a string.
To load XML from a stream
To populate an XML literal such as an XElement or XDocument object from a stream, you can use the method or the XNode.ReadFrom method.
The following code example shows the use of the ReadFrom method to populate an XDocument object with XML from an XML stream.
See also
Example
There are several ways interact with an Xml file.
- Xml Document
- XDocument
- XmlReader/XmlWriter
Before LINQ to XML we were used XMLDocument for manipulations in XML like adding attributes, elements and so on. Now LINQ to XML uses XDocument for the same kind of thing. Syntaxes are much easier than XMLDocument and it requires a minimal amount of code.
Also XDocument is mutch faster as XmlDocument. XmlDoucument is an old and dirty solution for query an XML document.
I am going to show some examples of XmlDocument class and XDocument class class:
Load XML file
XmlDocument
XDocument
Create XmlDocument
XmlDocument
XDocument
Change InnerText of node in XML
XmlDocument
XDocument
Save File after edit
Make sure to safe the xml after any change.
Retreive Values from XML
XmlDocument
XDocument
Retreive value from all from all child elements where attribute = something.
XmlDocument
XDocument
Append a node
XmlDocument
XDocument
Previous Next
XDocument.Load Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
Load(Stream) | Creates a new XDocument instance by using the specified stream. |
Load(TextReader) | Creates a new XDocument from a TextReader. |
Load(String) | Creates a new XDocument from a file. |
Load(XmlReader) | Creates a new XDocument from an XmlReader. |
Load(Stream, LoadOptions) | Creates a new XDocument instance by using the specified stream, optionally preserving white space, setting the base URI, and retaining line information. |
Load(TextReader, LoadOptions) | Creates a new XDocument from a TextReader, optionally preserving white space, setting the base URI, and retaining line information. |
Load(String, LoadOptions) | Creates a new XDocument from a file, optionally preserving white space, setting the base URI, and retaining line information. |
Load(XmlReader, LoadOptions) | Loads an XDocument from an XmlReader, optionally setting the base URI, and retaining line information. |
Using one of the overloads of this method, you can load an XDocument from a file, a TextReader, or an XmlReader.
To create an XDocument from a string that contains XML, use Parse.
Load(Stream)
Creates a new XDocument instance by using the specified stream.
Parameters
- stream
- Stream
The stream that contains the XML data.
Returns
- XDocument
An XDocument object that reads the data that is contained in the stream.
Remarks
If you want to control load options, use the Load overload that takes LoadOptions as a parameter.
The loading functionality of LINQ to XML is built upon XmlReader. Therefore, you might catch any exceptions that are thrown by the XmlReader.Create overload methods and the XmlReader methods that read and parse the document.
If you have to modify XmlReaderSettings, follow these steps:
Create an XmlReader by calling one of the Create overloads that take XmlReaderSettings as a parameter.
Pass the XmlReader to one of the Load overloads of XDocument that takes XmlReader as a parameter.
Load(TextReader)
Parameters
Returns
- XDocument
An XDocument that contains the contents of the specified TextReader.
Examples
The following example creates a document from a StringReader.
This example produces the following output:
Remarks
LINQ to XML's loading functionality is built upon XmlReader. Therefore, you might catch any exceptions that are thrown by the XmlReader.Create overload methods and the XmlReader methods that read and parse the document.
See also
Load(String)
Parameters
- uri
- String
A URI string that references the file to load into a new XDocument.
Returns
- XDocument
An XDocument that contains the contents of the specified file.
Examples
The following example shows how to load an XDocument from a file.
This example uses the following XML document:
Sample XML File: Typical Purchase Order (LINQ to XML)
This example produces the following output:
Remarks
This method uses an underlying XmlReader to read the XML into an XML tree.
Use Parse to create an XDocument from a string that contains XML.
LINQ to XML's loading functionality is built upon XmlReader. Therefore, you might catch any exceptions that are thrown by the XmlReader.Create overload methods and the XmlReader methods that read and parse the document.
See also
Load(XmlReader)
Parameters
Returns
- XDocument
An XDocument that contains the contents of the specified XmlReader.
Examples
The following example creates a DOM document, creates an XmlNodeReader from the DOM document, creates an XDocument using the XmlNodeReader.
This example produces the following output:
Remarks
One possible use for this method is to create a copy of a DOM document in a LINQ to XML tree. To do this, you create an XmlNodeReader from a DOM document, and then use the XmlNodeReader to create an XDocument.
LINQ to XML's loading functionality is built upon XmlReader. Therefore, you might catch any exceptions that are thrown by the XmlReader.Create overload methods and the XmlReader methods that read and parse the document.
See also
Load(Stream, LoadOptions)
Creates a new XDocument instance by using the specified stream, optionally preserving white space, setting the base URI, and retaining line information.
Parameters
- stream
- Stream
The stream containing the XML data.
Returns
- XDocument
An XDocument object that reads the data that is contained in the stream.
Remarks
The loading functionality of LINQ to XML is built upon XmlReader. Therefore, you might catch any exceptions that are thrown by the XmlReader.Create overload methods and the XmlReader methods that read and parse the document.
If you have to modify XmlReaderSettings, follow these steps:
Create an XmlReader by calling one of the Create overloads that takes XmlReaderSettings as a parameter.
Pass the XmlReader to one of the Load overloads of XDocument that takes XmlReader as a parameter.
Load(TextReader, LoadOptions)
Creates a new XDocument from a TextReader, optionally preserving white space, setting the base URI, and retaining line information.
Parameters
- options
- LoadOptions
A LoadOptions that specifies white space behavior, and whether to load base URI and line information.
Returns
- XDocument
An XDocument that contains the XML that was read from the specified TextReader.
Examples
The following example creates a document from a StringReader.
This example produces the following output:
Remarks
If the source XML is indented, setting the PreserveWhitespace flag in causes the reader to read all white space in the source XML. Nodes of type XText are created for both significant and insignificant white space.
If the source XML is indented, not setting the PreserveWhitespace flag in causes the reader to ignore all of the insignificant white space in the source XML. The XML tree is created without any text nodes for insignificant white space.
If the source XML is not indented, setting the PreserveWhitespace flag in has no effect. Significant white space is still preserved, and there are no spans of insignificant white space that could cause the creation of more white space text nodes.
For more information, see Preserve white space while loading or parsing XML and Preserve white space while serializing.
Use Parse to create an XElement from a string that contains XML.
Setting SetBaseUri is not valid when loading from a TextReader.
There is a performance penalty if you set the SetLineInfo flag.
The line information is accurate immediately after loading the XML document. If you modify the XML tree after loading the document, the line information may become meaningless.
LINQ to XML's loading functionality is built upon XmlReader. Therefore, you might catch any exceptions that are thrown by the XmlReader.Create overload methods and the XmlReader methods that read and parse the document.
See also
Load(String, LoadOptions)
Creates a new XDocument from a file, optionally preserving white space, setting the base URI, and retaining line information.
Parameters
- uri
- String
A URI string that references the file to load into a new XDocument.
- options
- LoadOptions
A LoadOptions that specifies white space behavior, and whether to load base URI and line information.
Returns
- XDocument
An XDocument that contains the contents of the specified file.
Examples
The following example shows how to load an XDocument from a file.
This example uses the following XML document:
Sample XML File: Typical Purchase Order (LINQ to XML)
This example produces the following output:
Remarks
If the source XML is indented, setting the PreserveWhitespace flag in causes the reader to read all white space in the source XML. Nodes of type XText are created for both significant and insignificant white space.
If the source XML is indented, not setting the PreserveWhitespace flag in causes the reader to ignore all of the insignificant white space in the source XML. The XML tree is created without any text nodes for insignificant white space.
If the source XML is not indented, setting the PreserveWhitespace flag in has no effect. Significant white space is still preserved, and there are no spans of insignificant white space that could cause the creation of more white space text nodes.
For more information, see Preserve white space while loading or parsing XML and Preserve white space while serializing.
Use Parse to create an XDocument from a string that contains XML.
There is a performance penalty if you set the SetBaseUri and the SetLineInfo flags.
The base URI and the line information are accurate immediately after loading the XML document. If you modify the XML tree after loading the document, the base URI and line information may become meaningless.
LINQ to XML's loading functionality is built upon XmlReader. Therefore, you might catch any exceptions that are thrown by the XmlReader.Create overload methods and the XmlReader methods that read and parse the document.
See also
Load(XmlReader, LoadOptions)
Loads an XDocument from an XmlReader, optionally setting the base URI, and retaining line information.
Parameters
Returns
- XDocument
An XDocument that contains the XML that was read from the specified XmlReader.
Examples
The following example loads the line information that it loads from the XmlReader. It then prints the line information.
This example produces the following output:
Remarks
By creating an XmlNodeReader from a DOM document, and then using the XmlNodeReader to create an XElement, this method can be used to create a copy of a DOM document in a LINQ to XML tree.
Use Parse to create an XDocument from a string that contains XML.
Setting PreserveWhitespace is not valid when loading from a XmlReader. The XmlReader will be configured to either read whitespace or not. The LINQ to XML tree will be populated with the whitespace nodes that the reader surfaces. This will be the behavior regardless of whether PreserveWhitespace is set or not.
The XmlReader may have a valid base URI or not. If you set SetBaseUri, the base URI will be set in the XML tree from the base URI that is reported by the XmlReader.
The XmlReader may have a valid line information or not. If you set SetLineInfo, the line information will be set in the XML tree from the line information that is reported by the XmlReader.
There is a performance penalty if you set the SetLineInfo flag.
The line information is accurate immediately after loading the XML document. If you modify the XML tree after loading the document, the line information may become meaningless.
LINQ to XML's loading functionality is built upon XmlReader. Therefore, you might catch any exceptions that are thrown by the XmlReader.Create overload methods and the XmlReader methods that read and parse the document.
See also
System.Xml.Linq.XDocument.Load(string)
42. Example
View licensepublic async static Task<IImmutableList<SyntaxNode>> GenerateMocksAsync( ILogSink logSink, Language language, string solutionPath, string xmlPath) { if (!File.Exists(xmlPath)) { var message = $"XML input file '{xmlPath}' no found."; logSink.Error(logSource, message); throw new IOException(message); } logSink.Info(logSource, "Loading XML input file '{0}'.", xmlPath); var document = XDocument.Load(xmlPath); var configuration = Configuration.FromXDocument(logSink, document); return await Generator.GenerateMocksAsync( logSink, language, solutionPath, configuration.GetInterfacePredicate(), configuration.GetNamespaceSelector(), configuration.GetNameSelector(), configuration.GetPlugins()); }Load xdocument
dotnet / runtime Public
XDocument.Load using LoadOptions.PreserveWhitespace does not preserve NewLine characters when paired with XDocument.Save using LoadOptions.SaveOptions.DisableFormatting.
Steps to test
- Create a new Console Application project.
- Replace the code in Program.cs with the code below
- Build and run the console application.
Expected Outcome
Both sets of XML roundtrip and match.
Actual Outcome
One set of XML fails to roundtrip. The line endings used in the input xml are replaced with the NewLine characters for the current Environment.
Then stick your finger in the hole and masturbate the hole with your finger, you love it. "I could only moan. I remembered his huge penis in my head. I remembered how he fucked me everywherewherever possible. in the park, entrance, car.
You will also like:
- Halloween wine charms
- Bakugou dancing animation
- Digital tv reviews
- Wood white bookcase
- Sprint store manteca
- Spirit monster tips
He is my wife's sponsor. Provides her with money, and in return sleeps with her. Gives her jewelry, underwear, expensive things. Let's imagine that I know about their relationship and, not against wife's sex with another man. His wife sleeps with him mainly because of money and, also, because he has a big dick.