Is this good XML?

I work for a client who used this XML structure for the content of his site:

<section title="title1" layoutType="VideoViewer" xmlPath="xml/ita/title1.xml" pageTitle="extended title" previewImg=""/>

<section title="another title" layoutType="TimeLine" xmlPath="xml/ita/timeline.xml" textFile="" pageTitle="extended title for timeline"></section>

I am new to XML, but it doesn’t look very good. Besides a different way to close tags (autocomplete with />or explicitly close with </section>), I would think that XML should be more like

<section> 
  <title>title1</title> 
  <layoutType>"VideoViewer" </layoutType>
  <xmlPath>xml/ita/title1.xml </xmlPath> 
  <pageTitle>extended title</pageTitle> 
  <previewImg/>
</section>

<section>
 ...etc etc..
</section>

What do you think?

If they are both ok, is there a "best practice"?

+3
source share
8 answers

As long as all the elements are enclosed in one root element, there is nothing wrong with it.

Any style of closing tag is acceptable, although I would recommend self-closing tags for all elements that do not have other tags inside them.

( ), , .

+6

, XML ? - -; , , -. - , , .

XML , .

+3

. .

.

XML, , , . .

: . , , / node.

, , .

+3

XML. , , .

-, :

  • .
  • .
  • XML; DOM .
  • , , , XPath (, node() | @*, XSLT) DOM (, .NET, XmlAttribute XmlNode, , XmlAttributeCollection XmlNodeList, XmlNode - , , 'new ).

:

  • , DOM , DOM .
  • , , .
  • , .
  • , .

/ . , /, , XML - , " .

( . WPF XAML , , . , - , Binding XAML, SelectedItem, , XamlReader , , XAML, SelectedItem Binding, . XAML , .)

. foo XmlDocument DOM ( #):

elm.SetAttribute("foo", value);

value = elm.GetAttribute("foo");

foo :

XmlElement fooElm = (XmlElement)elm.SelectSingleNode("foo");
if (fooElm == null)
{
   elm.OwnerDocument.CreateElement("foo");
   elm.AppendChild(fooElm);
}
fooElm.InnerText = value;

XmlElement fooElm = (XmlElement)elm.SelectSingleNode("foo");
value = fooElm != null ?? fooElm.InnerText : "";

, , ( , XDocument XmlDocument DOM ), .

, , ; , / . ; - , , .

Edit:

, , XamlReader , XAML, , Binding SelectedItem XAML, , , - XamlReader XAML. , , XML, XAML. KaXaml XAML, ? .

, XAML : :

<ListBox Binding="{...}" SelectedItem="..." ... />

:

<ListBox>
   <ListBox.Binding>...</ListBox.Binding>
   <ListBox.SelectedItem>...</ListBox.SelectedItem>
   ...
+2

, , . xml (, title= "blah" ) , , , (, , ..).

, , .

- "" ( , , ), , -

+1

, XML, W3 Schools. , , . .

+1

, , " ". , .

w3schools:

, . HTML. XML - . .

...

:

    * ( )
    * ( )
    * ( )

. . , .

.

+1

XML- , , . , 80 ( ), , .

, , , . , . , width, height format.

I use elements for longer values ​​(in particular, which can be of any length), especially those that can later be expanded into a more complex structure. For example, for an image link element, I would make child elements of the title, title, description, URL and other potentially long free text elements of the image link element.

XML must be easy to read. XML dialects should be reliable, and not change the system when changing. If in doubt, use items.

+1
source

Source: https://habr.com/ru/post/1723468/


All Articles